2014-09-25 30 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; 
     } 
    } 
}