views:

29

answers:

2

I have a table that I am displaying in a data grid view control. The user selects a single row from the control and presses a button. I need to retrieve the cells from that row and store them as strings.

Exactly how do I get the data using the SelectedRow method? I've been working on this for several hours and I'm at the end of my rope. Here's an example of something I've tried:

DataGridViewCellCollection selRowData = dataGridView1.SelectedRows[0].Cells;

If I try to access selRowData[x], the return value does not contain my data.

+1  A: 

You're close - you need to reference each Cell through its index and return its Value property:

string firstCellValue = dataGridView1.SelectedRows[0].Cells[0].Value;
string secondCellValue = dataGridView1.SelectedRows[0].Cells[1].Value;

etc.

Jay Riggs
A: 

Try using the Item element of the dgv.

dgvFoo.Item(0, dgvFoo.CurrentRow.Index).Value

That would return the value of the first item. You could put that into a for loop to get them all.

Another option would be to use the SelectedRows collection on the object and iterate through each selected row (or just the one in your case).

JonVD