Find DataRow in DataGridView

R

Rainer Queck

Hello NG,

what would be the best way to locate a DataRrow in a DataGridView?
I have by DataTable.Select a bunch of DataRows from a DataTable (which is
the data source to the DataGridView).
Now I would like to give those related DataGridViewRows a different
BackColor.

Thanks for hints and help!
Rainer
 
C

ClayB

If your DataTable has a primary key (and is also sorted by the key
column), then you can try using

int rowIndex =
DataTable.DefaultView.Find(someDataRow[primaryKey]);

to find the rowIndex.

==============
Clay Burch
Syncfusion, Inc.
 
R

Rainer Queck

Hi Clay,

ClayB said:
If your DataTable has a primary key (and is also sorted by the key
column), then you can try using

int rowIndex =
DataTable.DefaultView.Find(someDataRow[primaryKey]);

to find the rowIndex.

sorry for not being clear enough.
I do have the rowIndex of the datarows. But I realized, that "sorting" the
datagridview by clicking the header rearanges the view and therefore
DataGridViewRow <n> could be DataTableRow <i>.
I am looking for a way to identify the DataGridViewRow showing my
DataTableRow.

Regards
Rainer
 
K

Kevin Spencer

The DataGridView shows a View of your DataTable. Most likely, it is the
default DataView. You can use the DataView and the DataViewRows in its'
Collection to identify the underlying DataRows in the DataTable.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

Rainer Queck said:
Hi Clay,

ClayB said:
If your DataTable has a primary key (and is also sorted by the key
column), then you can try using

int rowIndex =
DataTable.DefaultView.Find(someDataRow[primaryKey]);

to find the rowIndex.

sorry for not being clear enough.
I do have the rowIndex of the datarows. But I realized, that "sorting" the
datagridview by clicking the header rearanges the view and therefore
DataGridViewRow <n> could be DataTableRow <i>.
I am looking for a way to identify the DataGridViewRow showing my
DataTableRow.

Regards
Rainer
 
J

Jeffrey Tan[MSFT]

Hi Rainer ,

Based on my understanding, you want to find the UI DataGridViewRows through
the datasource DataRowCollection.

Since the datasource can be bound to multiple UI controls, the datasource
objects(DataTable/DataView/DataRow) do not maintain any relation
information with the UI controls or databinding. So there is no easy smart
way of navigating from the datasource to the UI control rows.

To get the databinding relation information, we have to start from the
databound UI controls side. Normally, this requires us to enumerate all the
DataGridViewRows in the UI control to find its corresponding DataRow in
datasource, and then compare this DataRow with our DataRowCollection to
find out if this is the UI row we wanted to modify. Below is the code
snippet that demonstrates this logic:

DataTable dt = null;
DataRow[] dr_arr = null;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));


for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();
dt.Rows.Add(dr);
}

this.dataGridView1.DataSource = dt;

dr_arr = dt.Select("column2='item3'");
}

private void button1_Click(object sender, EventArgs e)
{
if (dr_arr != null && dr_arr.Length > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRowView drv = this.dataGridView1.Rows.DataBoundItem as
DataRowView;
for (int j = 0; j < dr_arr.Length; j++)
{
if (drv.Row.Equals(dr_arr[j]))
{
this.dataGridView1.Rows.DefaultCellStyle.BackColor =
Color.Red;
}
}
}
}
}
Note: in the code snippet, after populating the DataTable, I use Select
method to get the 3rd DataRow to dr_arr collection.

The key point of the code is using DataGridViewRow.DataBoundItem to get the
corresponding DataRowView, then use DataRowView.Row to get the real
DataRow.

This sample code works well on my side. Hope it helps.

Sure, if your DataTable has any unique value DataColumn, the code can be
somewhat easier. We can directly retrieve the unique cell value in the
underlying DataRow collection and enumerate through DataGridViewCell in
corresponding UI column(for comparing values) to identify the UI row index.
If you need any code snippet or sample in this scenario, please feel free
to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

Rainer Queck

Hello Jeffrey,

Thank you VERY much for your help!
Your explanations hit my question to 100% and your sample code is exactly
what I was looking for.

Regards
Rainer Queck

"Jeffrey Tan[MSFT]" said:
Hi Rainer ,

Based on my understanding, you want to find the UI DataGridViewRows
through
the datasource DataRowCollection.

Since the datasource can be bound to multiple UI controls, the datasource
objects(DataTable/DataView/DataRow) do not maintain any relation
information with the UI controls or databinding. So there is no easy smart
way of navigating from the datasource to the UI control rows.

To get the databinding relation information, we have to start from the
databound UI controls side. Normally, this requires us to enumerate all
the
DataGridViewRows in the UI control to find its corresponding DataRow in
datasource, and then compare this DataRow with our DataRowCollection to
find out if this is the UI row we wanted to modify. Below is the code
snippet that demonstrates this logic:

DataTable dt = null;
DataRow[] dr_arr = null;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add(new DataColumn("column1", typeof(int)));
dt.Columns.Add(new DataColumn("column2", typeof(string)));


for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr["column1"] = i;
dr["column2"] = "item" + i.ToString();
dt.Rows.Add(dr);
}

this.dataGridView1.DataSource = dt;

dr_arr = dt.Select("column2='item3'");
}

private void button1_Click(object sender, EventArgs e)
{
if (dr_arr != null && dr_arr.Length > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRowView drv = this.dataGridView1.Rows.DataBoundItem as
DataRowView;
for (int j = 0; j < dr_arr.Length; j++)
{
if (drv.Row.Equals(dr_arr[j]))
{
this.dataGridView1.Rows.DefaultCellStyle.BackColor =
Color.Red;
}
}
}
}
}
Note: in the code snippet, after populating the DataTable, I use Select
method to get the 3rd DataRow to dr_arr collection.

The key point of the code is using DataGridViewRow.DataBoundItem to get
the
corresponding DataRowView, then use DataRowView.Row to get the real
DataRow.

This sample code works well on my side. Hope it helps.

Sure, if your DataTable has any unique value DataColumn, the code can be
somewhat easier. We can directly retrieve the unique cell value in the
underlying DataRow collection and enumerate through DataGridViewCell in
corresponding UI column(for comparing values) to identify the UI row
index.
If you need any code snippet or sample in this scenario, please feel free
to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top