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.