Index of Currently Selected Row in DataGridView

后端 未结 12 1760
误落风尘
误落风尘 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:01

    try this it will work...it will give you the index of selected row index...

    int rowindex = dataGridView1.CurrentRow.Index;
    MessageBox.Show(rowindex.ToString());
    
    0 讨论(0)
  • 2020-11-28 23:01

    try this

    bool flag = dg1.CurrentRow.Selected;
    
    if(flag)
    {
      /// datagridview  row  is  selected in datagridview rowselect selection mode
    
    }
    else
    {
      /// no  row is selected or last empty row is selected
    }
    
    0 讨论(0)
  • 2020-11-28 23:02

    You can try this code :

    int columnIndex = dataGridView.CurrentCell.ColumnIndex;
    int rowIndex = dataGridView.CurrentCell.RowIndex;
    
    0 讨论(0)
  • 2020-11-28 23:06

    Try the following:

    int myIndex = MyDataGrid.SelectedIndex;
    

    This will give the index of the row which is currently selected.

    Hope this helps

    0 讨论(0)
  • 2020-11-28 23:08
    dataGridView1.SelectedRows[0].Index;
    

    Here find all about datagridview C# datagridview tutorial

    Lynda

    0 讨论(0)
  • 2020-11-28 23:10
    dataGridView1.SelectedRows[0].Index;
    

    Or if you wanted to use LINQ and get the index of all selected rows, you could do:

    dataGridView1.SelectedRows.Select(r => r.Index);
    
    0 讨论(0)
提交回复
热议问题