Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# winform multiple select grid row并获取所选行的行索引_C#_Winforms_Datagridview - Fatal编程技术网

C# winform multiple select grid row并获取所选行的行索引

C# winform multiple select grid row并获取所选行的行索引,c#,winforms,datagridview,C#,Winforms,Datagridview,我的winform中有一个数据绑定的gridview。我想知道如何获取当前选定行(即多行)的索引。 我可以用一行来做这件事。但是有没有一种方法可以让我有一个复选框或者一些我可以索引多行的东西呢。 下图将帮助您更好地了解我的要求 使用属性获取所有选定行 要选择多行,请执行以下操作: DataGridView.MultiSelect = true; 首先设置cellcontent单击DataGridView事件 dataGridView.CellContentClick += new System

我的winform中有一个数据绑定的gridview。我想知道如何获取当前选定行(即多行)的索引。 我可以用一行来做这件事。但是有没有一种方法可以让我有一个复选框或者一些我可以索引多行的东西呢。 下图将帮助您更好地了解我的要求

使用属性获取所有选定行

要选择多行,请执行以下操作:

DataGridView.MultiSelect = true;

首先设置
cellcontent单击
DataGridView
事件

dataGridView.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.onCellContentClick);
对于每个单元格,单击它将调用以下方法。在这里,您可以创建一个列表并用单击的行索引填充它

public void onCellContentClick(DataGridViewCellEventArgs cell)
{
   // Check whether selected cell is check box column, here 0 indicates the check box column.
   if (cell.ColumnIndex == 0)   
   {
      bool isChecked = (Boolean) dataGridView[cell.ColumnIndex, cell.RowIndex].EditedFormattedValue;

      if(isChecked) 
      {
        // Below will give you the selected cell row index, for multiple rows you can populate those index in list or whatever you convenient with.
        cell.RowIndex;
      }
    }
}