Link to home
Start Free TrialLog in
Avatar of mms_master
mms_masterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

DataGridView - get value from cell 0 in selected row.

Hi,

I want to get the value from the first cell (i.e. index 0) in the selected row of a DataGridView. The code Im using at the moment is:

dgvCurrentJobs.CurrentRow.Cells(0).Value.ToString

I've also tried:

dgvCurrentJobs.SelectedRows.Item(0).Cells(0).Value.ToString

Both of the above work in respect of returning a value from cell 0. But, no matter which row I select, when this line of code is run, it deselects my selection, selects the first row (Index 0) and returns the value in cell 0 of that row, and not the one I had selected. If that makes sense.

Example:

Column 0 |    Column 1
---------------------------
R0C0       |    R0C1
R1C0       |    R1C1
R2C0       |    R2C1

If the table above is my DataGridView, and I select the second row which contains "R1C0" in column index 0 and "R1C1" in column index 1; When I press the button that runs the code I mentioned above, the first row containing "R0C0" and "R0C1" becomes highlighted and the value in my msgbox is "R0C0".

Not sure how easy to understand that is, let me know if you cant understand it.

Thanks in advance
mms_master
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Try this....
DataGridView1(0, DataGridView1.CurrentCell.RowIndex).Value

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mms_master
mms_master
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial