DevExpress gridview 添加全选

1、通过check多选框选择

给当前数据源添加一条,列名Check 然后在gridview添加一列绑定Check列名

dtOrderBind.Columns.Add("Check", System.Type.GetType("System.Boolean"));

指定全选列(Check)的imageIndex默认图片(用微软Imagelist控件)

以下是gridview点击事件

View Code
 #region 事件:订单全选
private void grvPrintOrder_Click(object sender, EventArgs e)
{
Point point = this.grPrintOrder.PointToClient(Cursor.Position);
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hitInfo = this.grvPrintOrder.CalcHitInfo(point);
if (hitInfo.InColumnPanel == true)
{
string s = hitInfo.Column.FieldName;
//判断当前当击列是否是全选列
if (s == "Check")
{
if (Convert.ToBoolean(hitInfo.Column.Tag) == false)
{
hitInfo.Column.Tag = true;
hitInfo.Column.ImageIndex = 1;
ChangeChk = true;
ChkSelect();
}
else
{
hitInfo.Column.Tag = false;
hitInfo.Column.ImageIndex = 0;
ChangeChk = false;
ChkSelect();
}
}
}
}
#endregion

得到当前选中先

View Code
this.grPrintOrder.FocusedView.CloseEditor();
this.grPrintOrder.FocusedView.UpdateCurrentRow();
string value = "";
for (int i = 0; i < this.grvPrintOrder.RowCount; i++)
{
value = this.grvPrintOrder.GetDataRow(i)["Check"].ToString();
if (value.ToLower() == "true")
{
if (!list.Contains(this.grvPrintOrder.GetRowCellValue(i, "id").ToString()))
{
list.Add(this.grvPrintOrder.GetRowCellValue(i, "id").ToString());
}
}
}

this.grPrintOrder.FocusedView.CloseEditor();

this.grPrintOrder.FocusedView.UpdateCurrentRow();

这二行代码一定要加上,不然得不到最后一个选择。

2、通过按住shift或ctrl多选

首先设置将gridview设置为多选

  // 设置为多选

this.grvPrintOrder.OptionsSelection.MultiSelect = true;

this.grvPrintOrder.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect;

得到当前用户选择的行

View Code
  int[] grvarry= this.gridView1.GetSelectedRows();     
for (int i = 0; i < grvarry.Length;i++ )
{
DataRow row = this.gridView1.GetDataRow(grvarry[i]);
if(row!=null)
{
XtraMessageBox.Show(row["comid"].ToString());
}
}
this.gridView1.MoveNext();



  



原文地址:https://www.cnblogs.com/freexiaoyu/p/2219817.html