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