Dev+Grid复选框(全选,反选)
DevExpress+Grid 全选反选记录
一、先建一个辅助类
public class GridControlHelp { /// <summary> /// draw checkbox /// </summary> /// <param name="e"></param> /// <param name="chk"></param> public static void DrawCheckBox(DevExpress.XtraGrid.Views.Grid.ColumnHeaderCustomDrawEventArgs e, bool chk) { DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit; if (repositoryCheck != null) { System.Drawing.Graphics g = e.Graphics; System.Drawing.Rectangle r = e.Bounds; DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo info; DevExpress.XtraEditors.Drawing.CheckEditPainter painter; DevExpress.XtraEditors.Drawing.ControlGraphicsInfoArgs args; info = repositoryCheck.CreateViewInfo() as DevExpress.XtraEditors.ViewInfo.CheckEditViewInfo; painter = repositoryCheck.CreatePainter() as DevExpress.XtraEditors.Drawing.CheckEditPainter; info.EditValue = chk; 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(); } } /// <summary> /// click checkbox event /// </summary> /// <param name="gridView"></param> /// <param name="fieldName"></param> /// <param name="currentStatus"></param> /// <returns></returns> public static bool ClickGridCheckBox(DevExpress.XtraGrid.Views.Grid.GridView gridView, string fieldName, bool currentStatus) { bool result = false; if (gridView != null) { //禁止排序 gridView.ClearSorting(); gridView.PostEditor(); DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo info; System.Drawing.Point pt = gridView.GridControl.PointToClient(Control.MousePosition); info = gridView.CalcHitInfo(pt); if (info.InColumn && info.Column != null && info.Column.FieldName == fieldName) { for (int i = 0; i < gridView.RowCount; i++) { gridView.SetRowCellValue(i, fieldName, !currentStatus); } return true; } } return result; } }
二、需要用到Grid控件的几个事件
gvBk_Click控件click事件
gvBk_CustomDrawColumnHeader表头重绘事件
gvBk_DataSourceChanged数据源改变事件
gvBk_RowCellClick单元格点击事件
bool checkStatus = true;默认是否选中状态
1.数据源中添加选中状态字段Bool类型,True选中,false取消选中
DataTable table = EdiBookingBiz.GetBookingBySchid(ssd, user);//用vessel和voyage去找booking if (table.Rows.Count > 0) { table.Columns.Add("Chk", System.Type.GetType("System.Boolean")); table.Columns["Chk"].DefaultValue = Boolean.FalseString; }
bool checkStatus = true; private void gvBk_Click(object sender, EventArgs e) { if (GridControlHelp.ClickGridCheckBox(gvBk, "Chk", checkStatus)) { checkStatus = !checkStatus; } } private void gvBk_CustomDrawColumnHeader(object sender, ColumnHeaderCustomDrawEventArgs e) { if (e.Column != null && e.Column.FieldName == "Chk") { e.Info.InnerElements.Clear(); e.Painter.DrawObject(e.Info); GridControlHelp.DrawCheckBox(e, checkStatus); e.Handled = true; } } private void gvBk_DataSourceChanged(object sender, EventArgs e) { GridColumn column = this.gvBk.Columns.ColumnByFieldName("Chk"); if (column != null) { column.Width = 80; column.OptionsColumn.ShowCaption = false; column.ColumnEdit = new RepositoryItemCheckEdit(); } } private void gvBk_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e) { string columnName = gvBk.FocusedColumn.FieldName.ToString(); if (columnName == "Chk") { DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit repositoryCheck = e.Column.ColumnEdit as DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit; string value = gvBk.GetFocusedRowCellValue(columnName).ToString(); int hand = e.RowHandle; if (value == "True") { gvBk.SetRowCellValue(hand, "Chk", false); } else { gvBk.SetRowCellValue(hand, "Chk", true); } //修改后的值更新到数据源 gvBk.CloseEditor(); gvBk.UpdateCurrentRow(); } } //默认全选的方法 private void SelectAll(DataTable tableSource) { if (!checkStatus) checkStatus = true; if (tableSource != null && tableSource.Rows.Count > 0) { foreach (DataRow item in tableSource.Rows) { item["Chk"] = true; } gridBk.DataSource = tableSource; } }