Hi

I'm having a real headache getting the selected row from my datagrid (only one row can be selected at a time).

If I use the callback function from clicking on the data grid I can get the selected row no problem

dgStaffList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int selectedRow = e.RowIndex;
}

However I want other methods to be able to read the cell contents without any user interaction.

Any idea how to do this?

try this:

int rowSelected = dataGridView1.CurrentCell.RowIndex;

You can also reference the cells by their column names, like this:

string myValue = dgStaffList.SelectedRows[0].Cells["FirstName"].Value.ToString();

SelectedRows[] is 0 because you only have one row selected. You can also reference the cell by column position instead of name, just substitute a 0-indexed integer for the column number in the place of "FirstName".

can use this line of code also

dgStaffList.Rows[e.index].Cells["column_name"].Value.ToString();

Thanks peeps got it working :D

and your solution is... ?

int rowSelected = dataGridView1.CurrentCell.RowIndex;

worked a dream

:) I thought it has to work.
bye
And close the thread (mark as answered).
Mitja

Ah ok will do sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.