参考DEV官网代码做了一个增加checkbox列效果:
#region 方法:设置GridView数据绑定
public void GridDataBind()
{
dt = dbHelper.Fill(cmdStr); // 填充DataTable
dt.Columns.Add("Check", typeof(bool)); // 增加个CheckBox列
dt.Columns["Check"].SetOrdinal(0); // 设置选择列的位置
for (int i = 0; i < dt.Rows.Count; i++)
{
dt.Rows[i]["Check"] = false;
}
gridControl1.DataSource = dt;
gridView1.Columns["Check"].Width = 22;
gridView1.Columns["Check"].OptionsColumn.ShowCaption = false;
gridView1.Columns["Check"].OptionsColumn.AllowSort = DevExpress.Utils.DefaultBoolean.False;
}
#endregion
===================================================================================
#region GridView增加CheckBox列相关代码
protected void DrawCheckBox(Graphics g, Rectangle r, bool Checked)
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info;
DevExpress.XtraEditors.Drawing.CheckEditPainter painter;
DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args;
info = chkEdit.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo;
painter = chkEdit.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter;
info.EditValue = Checked;
info.Bounds = r;
info.PaintAppearance.ForeColor = Color.Black;
info.CalcViewInfo(g);
args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
private void gridView_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == (sender as GridView).Columns["Check"])
{
e.Info.InnerElements.Clear();
e.Info.Appearance.ForeColor = Color.Blue;
e.Painter.DrawObject(e.Info);
DrawCheckBox(e.Graphics, e.Bounds, GetCheckedCount() == gridView1.DataRowCount);
//DrawCheckBox(e.Graphics, e.Bounds, false);
e.Handled = true;
}
}
private void gridView_MouseUp(object sender, MouseEventArgs e)
{
if (e.Clicks == 1 && e.Button == MouseButtons.Left)
{
GridHitInfo gridHitInfo;
Point point = gridView1.GridControl.PointToClient(Control.MousePosition);
gridHitInfo = gridView1.CalcHitInfo(point);
if (gridHitInfo.InColumn && gridHitInfo.Column.FieldName == "Check")
{
if (GetCheckedCount() == gridView1.DataRowCount)
{
UnCheckAll();
}
else
{
CheckAll();
}
}
if (gridHitInfo.InRow && gridHitInfo.InRowCell == true)
{
if (Convert.ToBoolean(gridView1.GetFocusedRowCellValue("Check")) == false)
{
CheckSingle();
}
else
{
UnCheckSingle();
}
}
}
}
private int GetCheckedCount() // 获取到已选择的数量
{
int count = 0;
for (int i = 0; i < gridView1.DataRowCount; i++)
{
if ((bool)gridView1.GetRowCellValue(i, gridView1.Columns["Check"]) == true)
{
count++;
}
}
return count;
}
private void CheckAll() // 选择全部
{
for (int i = 0; i < gridView1.DataRowCount; i++)
{
gridView1.SetRowCellValue(i, gridView1.Columns["Check"], true);
}
}
private void UnCheckAll() // 反选全部
{
for (int i = 0; i < gridView1.DataRowCount; i++)
{
gridView1.SetRowCellValue(i, gridView1.Columns["Check"], false);
}
}
private void CheckSingle() // 选择一个
{
gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Check"], true);
}
private void UnCheckSingle() // 反选一个
{
gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["Check"], false);
}
#endregion
======================================================================
在窗体加载事件里的代码:
private void MainForm_Load(object sender, EventArgs e)
{
#region 从本地数据库中获取数据 并展示到gridView控件上
GridDataBind();
#endregion
#region 设置gridview列只读(不能编辑)
for (int i = 1; i < gridView1.Columns.Count; i++)
{
gridView1.Columns[i].OptionsColumn.ReadOnly = true;
}
#endregion
}