116

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

apdevelop
  • 598
  • 6
  • 19
ErikE
  • 48,881
  • 23
  • 151
  • 196

12 Answers12

198

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.

fletcher
  • 13,380
  • 9
  • 52
  • 69
  • 1
    Perfect. This seems simplest/fastest, even over SelectedRows[0].Index. Would that be a good assumption? – ErikE Aug 26 '10 at 18:24
  • Hmm, I didn't think about multiple rows being selected. I'm not sure of the behaviour of current cell in this case. – fletcher Aug 26 '10 at 18:27
  • 1
    If it's anything like Excel, the selected list can be many rows/columns, but there is only one current/active cell. I only care about the current row so this should do fine. – ErikE Aug 26 '10 at 18:34
  • 20
    I know this post is old, but it might help someone else : `CurrentCell` returns the "active" cell, which is different from "Selected". Even if there are multiple rows selected, the active cell might be somewhere else, and there can be only one active cell – Luke Marlin Apr 08 '13 at 13:32
  • 3
    And also, even if you de-select everything in the data grid view, one of the cells will still be active, so you can't rely on this being null or something like that for there being no rows selected. – Daniel Gray Dec 23 '14 at 17:12
  • If you change the selected row or cell programatically this will NOT work, you have to use `yourDGV.SelectedRows[0].Index;` as Jay Riggs answered below. – Molx Dec 05 '17 at 00:07
  • Like others pointed out, CurrentCell does not represent user selection. Jay Riggs's answer is the correct one which should be accepted by OP. – AecorSoft May 12 '20 at 02:11
43

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

int index = yourDGV.SelectedRows[0].Index;
Jay Riggs
  • 53,046
  • 9
  • 139
  • 151
8
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);
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • Helpful (IndexOf was not really "on my radar" yet), but roundabout since getting the row isn't necessary. The .Index method is what I was looking for. – ErikE Aug 26 '10 at 18:22
6
dataGridView1.SelectedRows[0].Index;

Here find all about datagridview C# datagridview tutorial

ErikE
  • 48,881
  • 23
  • 151
  • 196
Lyndaeldo
  • 61
  • 1
  • 1
4

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

int rowindex = dataGridView1.CurrentRow.Index;
MessageBox.Show(rowindex.ToString());
Majid
  • 13,853
  • 15
  • 77
  • 113
sanjeev
  • 590
  • 1
  • 11
  • 21
3

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;
}
Allan Ruin
  • 5,229
  • 7
  • 37
  • 42
2

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
}
HyperNova
  • 21
  • 2
  • Thanks, exactly what I needed to determine what the hidden value of a selected row when using a control that wasn't associated with the gridview. – orgtigger Oct 09 '14 at 23:05
1

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.

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
  • 3
    Thanks for chipping in, but your answer is a duplicate of the selected (and most upvoted answer) which gave the solution `datagridview.CurrentCell.RowIndex`. My apologies—I am going to recommend that this post be deleted as a full duplicate. – ErikE Nov 25 '15 at 02:08
1

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 )

Kilanash
  • 4,479
  • 1
  • 14
  • 11
  • P.S. since I asked for the row index, things would have been less rocky in our relationship if you'd said `DataGridView.CurrentCellAddress.Y` ... :) – ErikE Aug 26 '10 at 19:13
  • I think at this point all of these answers are valid, it's just up to you which one you want to choose that's cleanest for your purposes. I'd suggest typing up the different implementations and looking at the IL in .NET Reflector (http://www.red-gate.com/products/reflector/) if you really want to see what code gets generated for each, but as many will say, that's micro-optimization. It's really down to what's most clear in intent. PS Note taken to be more clear in future. – Kilanash Aug 26 '10 at 19:35
1

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
}
goto
  • 7,908
  • 10
  • 48
  • 58
Han Nguyen
  • 11
  • 3
0

You can try this code :

int columnIndex = dataGridView.CurrentCell.ColumnIndex;
int rowIndex = dataGridView.CurrentCell.RowIndex;
Nima Derakhshanjan
  • 1,380
  • 9
  • 24
  • 37
-2

Try the following:

int myIndex = MyDataGrid.SelectedIndex;

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

Hope this helps

P_Fitz
  • 819
  • 9
  • 16