2014-09-25 85 views
1

我的winform中有一个databound gridview。我想知道如何获得当前选定行的索引,即多行。 我可以用一行来做到这一点。但有没有办法我可以有一个复选框或其中我可以索引多行。 下面的图片将帮助你更好地理解我的要求。winform多选网格行并获得选定行的行索引

enter image description here

回答

1

首先设置CellContentClick事件您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; 
     } 
    } 
}