0

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?

4
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by 303factory
0

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

0
int rowSelected = dataGridView1.CurrentCell.RowIndex;

worked a dream

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.