Index of Currently Selected Row in Datagridview

Index of Currently Selected Row in DataGridView

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.

How to get the current rowindex of a DataGridView?

Catching the current row in a DataGridView is really quite simple and you have posted two ways which work just fine:

int currentRow = datagridview.CurrentCell.RowIndex;

or:

int currentRow = datagridview.CurrentRow.Index

The third one is actually rather problematatic as, depending on the SelectionMode of the DataGridView the current row may not be selected.

But your problems come from trying to grab the index in response to the user hitting the Enter-key.

This by default will move the current cell one row down, if there is one. So the behaviour will vary between the last and the other rows..

If there isn't a 'next' row, the current cell will either stay where it is or, if AllowUserToAddRows is true, the DGV will create a new, empty row and move there.

So if you always want to get the current index without moving the current cell you need to prevent the processing of the Enter-key.

Here is one way to do that:

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
// don't pass the enter key on to the DGV:
e.Handled = true;
// now store or proecess the index:
Console.WriteLine(dataGridView1.CurrentRow + "");
}
}

The user will still be able to move around with the cursor keys.

how to get index of the last selected row from a selection of rows in a DataGridView

Sorry, I'm adding answer for myself. There could be other faster ways, but this works.

            List<int> lst = new List<int>();
foreach (DataGridViewRow row in dg.SelectedRows)
lst.Add(row.Index);

lst.Sort();
int i = lst[lst.Count - 1];

What this does is add the indexes of all selected rows to an List<> and then do a sort and then give the last item from the sorted List<>.

Note: the problem with Bala R's method is that, it depends on the order the rows were selected (ie, where the selected pointer lies). It fails when selected rows aren't in an order. It gives the row that was selected last, not necessarily the maximum most index from a selection of rows..

Thanks everyone!

DataGridView row index always returns the same value

The real problem was DeleteItem property in my BindingNavigator!

Before I set it to "None" it would delete the row from my DataGridView before the execution of "btnDeleteDjelatnik_Click" and mess up the indexation so I couldn't get the real ID of the row I wanted to delete!

My method now looks like this:

private void btnDeleteDjelatnik_Click(object sender, EventArgs e)
{
int index = djelatnikDataGrid.SelectedRows[0].Index;
int idDjelatnik = -1;
Int32.TryParse(djelatnikDataGrid["IDDjelatnik", index].Value.ToString(), out idDjelatnik);
r.DeleteDjelatnik(idDjelatnik);
djelatnikDataGrid.Rows.RemoveAt(index);
}

Thanks for help everyone!

Data Grid View...programmatically setting the select row index doesn't set the CurrentRow.Index to the same?

CurrentRow is the row containing the currently active cell. When you bind the DataGridView to an external data source, this property is reset to its default value which is the first cell in the first column.

SelectedRow is the row which is currently selected/highlighted. It may be one or more rows depending on MultiSelect property. To select a row you have to set its Selected property to true.

By setting the row as selected you merely keeping it highlighted not making it active.

To retain the current cell you have to store the Current cell's row and column index. To get them use the CurrentCellAddress property. Once your refresh the DataSource set the CurrentCell property using these indexes.

dataGridView1.CurrentCell = dataGridView1.Rows(rowindex).Cells(columnindex);

How to empty the current row of a DataGridView in C#?

I always use removeat by first checking to ensure something is selected and then do a removeat while passing the index.

if(dataGridView1.CurrentRow.Index > -1 && !dataGridView1.Rows[dataGridView1.CurrentRow.Index].IsNewRow)
dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);

EDIT: Added a check for IsNewRow in case AllowUsersToAddRows is True which would throw and exception if that was the selected row.



Related Topics



Leave a reply



Submit