Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using a databound WinForms DataGridView; how do I go from a user selected row in the DGV to the DataRow of the DataTable that is its source?
Posted

Try something like this...

DataRow row = (dataGridView1.Rows[index].DataBoundItem as DataRowView).Row;

where dataGridView1 is the name of your grid and
index is the index of the DataGridViewRow that you are working with.
 
Share this answer
 
And if the dataGridView1 is not databound?
 
Share this answer
 
I figured out the not databound access.

string const Column1Title = "Description";
string const Column2Title = "Value";
for(int index = 0; index < dataGridView1.Rows.Count; ++index)
{
DataGridViewRow dataRow = dataGridView1.Rows[index];
if (dataRow.IsNewRow)
continue;
string desc = dataRow.Cells[Column1Title].Value.ToString();
string val = dataRow.Cells[Column2Title].Value.ToString();
//etc.
}

Thanks for pointing me in the right direction! :D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900