Index of Currently Selected Row in DataGridView

后端 未结 12 1761
误落风尘
误落风尘 2020-11-28 22:46

It\'s that simple. How do I get the index of the currently selected Row of a DataGridView? I don\'t want the Row object, I want the in

相关标签:
12条回答
  • 2020-11-28 23:12

    I used if get row value is clicked:

    private void dataGridView_Product_CellClick(object sender, DataGridViewCellEventArgs e){
        int rowIndex;
        //rowIndex = e.RowIndex; //Option 1
        //rowIndex= dataGridView_Product.CurrentCell.RowIndex; //Option 2
        rowIndex = dataGridView_Product.CurrentRow.Index; //Option 3
    }
    
    0 讨论(0)
  • 2020-11-28 23:16

    Use the Index property in your DGV's SelectedRows collection:

    int index = yourDGV.SelectedRows[0].Index;
    
    0 讨论(0)
  • 2020-11-28 23:18

    I modified @JayRiggs' answer, and this works. You need the if because sometimes the SelectedRows may be empty, so the index operation will throw a exception.

    if (yourDGV.SelectedRows.Count>0){
        int index = yourDGV.SelectedRows[0].Index;
    }
    
    0 讨论(0)
  • 2020-11-28 23:25

    There is the RowIndex property for the CurrentCell property for the DataGridView.

    datagridview.CurrentCell.RowIndex
    

    Handle the SelectionChanged event and find the index of the selected row as above.

    0 讨论(0)
  • 2020-11-28 23:26

    Try it:

    int rc=dgvDataRc.CurrentCell.RowIndex;** //for find the row index number
    MessageBox.Show("Current Row Index is = " + rc.ToString());
    

    I hope it will help you.

    0 讨论(0)
  • 2020-11-28 23:27

    Try DataGridView.CurrentCellAddress.

    Returns: A Point that represents the row and column indexes of the currently active cell.

    E.G. Select the first column and the fifth row, and you'll get back: Point( X=1, Y=5 )

    0 讨论(0)
提交回复
热议问题