GridView에 CheckBox를 셋팅 및 관련 Event등록.
var itemIndex = gridControl.RepositoryItems.Add(new RepositoryItemCheckEdit());
var columnEdit = gridControl.RepositoryItems[itemIndex] as RepositoryItemCheckEdit;
columnEdit.ValueChecked = true;
columnEdit.ValueUnchecked = false;
gridView.Columns[0].ColumnEdit = columnEdit;
this.gridView.CustomDrawColumnHeader += gridView_CustomDrawColumnHeader;
GridView Column Header에 Checkbox를 그려주고, 체크동작을 하기위한 이벤트를 구현한다.
void gridView_CustomDrawColumnHeader(object sender, DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e)
{
if (e.Column == null)
return;
if (e.Column.AbsoluteIndex != 0)
return;
Rectangle rect = e.Bounds;
rect.Inflate(-1, -1);
e.Info.InnerElements.Clear();
e.Painter.DrawObject(e.Info);
DrawCheckBox(e.Graphics, rect, e.Column.ColumnEdit as RepositoryItemCheckEdit, IsAllSelected());
e.Handled = true;
}
void DrawCheckBox(Graphics g, Rectangle r, RepositoryItemCheckEdit checkEdit, bool isCheck)
{
DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info = default(DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo);
DevExpress.XtraEditors.Drawing.CheckEditPainter painter = default(DevExpress.XtraEditors.Drawing.CheckEditPainter);
DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args = default(DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs);
info = (CheckEditViewInfo)checkEdit.CreateViewInfo();
painter = (CheckEditPainter)checkEdit.CreatePainter();
info.EditValue = isCheck;
info.Bounds = r;
info.CalcViewInfo(g);
args = new DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs(info, new DevExpress.Utils.Drawing.GraphicsCache(g), r);
painter.Draw(args);
args.Cache.Dispose();
}
void gridView_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left && e.Clicks == 1)
{
GridView gridview = sender as GridView;
if (gridview != null)
{
GridHitInfo hitinfo = gridview.CalcHitInfo(e.Location);
if (hitinfo.InRow == false && hitinfo.InColumn == true)
{
SetAllCheckBox(!IsAllSelected());
if (AffectCheckBoxChange != null)
AffectCheckBoxChange(this, null);
}
}
this.gridView.RefreshData();
DXMouseEventArgs args = DXMouseEventArgs.GetMouseArgs(e);
args.Handled = true;
}
}