Index of Currently Selected Row in DataGridView

C#.NetWinformsDatagridview

C# Problem Overview


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 index (0 .. n).

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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

int index = yourDGV.SelectedRows[0].Index;

Solution 3 - C#

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);

Solution 4 - C#

dataGridView1.SelectedRows[0].Index;

Here find all about datagridview C# datagridview tutorial

Lynda

Solution 5 - C#

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

int rowindex = dataGridView1.CurrentRow.Index;
MessageBox.Show(rowindex.ToString());

Solution 6 - C#

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
}

Solution 7 - C#

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;
}

Solution 8 - C#

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.

Solution 9 - C#

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 )

Solution 10 - C#

You can try this code :

int columnIndex = dataGridView.CurrentCell.ColumnIndex;
int rowIndex = dataGridView.CurrentCell.RowIndex;

Solution 11 - C#

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
}

Solution 12 - C#

Try the following:

int myIndex = MyDataGrid.SelectedIndex;

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

Hope this helps

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionErikEView Question on Stackoverflow
Solution 1 - C#fletcherView Answer on Stackoverflow
Solution 2 - C#Jay RiggsView Answer on Stackoverflow
Solution 3 - C#Justin NiessnerView Answer on Stackoverflow
Solution 4 - C#LyndaeldoView Answer on Stackoverflow
Solution 5 - C#sanjeevView Answer on Stackoverflow
Solution 6 - C#HyperNovaView Answer on Stackoverflow
Solution 7 - C#Allan RuinView Answer on Stackoverflow
Solution 8 - C#Raaz SalyaniView Answer on Stackoverflow
Solution 9 - C#KilanashView Answer on Stackoverflow
Solution 10 - C#subhankarView Answer on Stackoverflow
Solution 11 - C#Han NguyenView Answer on Stackoverflow
Solution 12 - C#P_FitzView Answer on Stackoverflow