By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,435 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

Find DataRow in DataGridView

P: n/a
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
Feb 12 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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.

Feb 12 '07 #2

P: n/a
Hi Clay,

"ClayB" <cl***@syncfusion.comschrieb im Newsbeitrag
news:11**********************@k78g2000cwa.googlegr oups.com...
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 <ncould be DataTableRow <i>.
I am looking for a way to identify the DataGridViewRow showing my
DataTableRow.

Regards
Rainer
Feb 12 '07 #3

P: n/a
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" <Ra****@noemail.noemailwrote in message
news:uq*************@TK2MSFTNGP06.phx.gbl...
Hi Clay,

"ClayB" <cl***@syncfusion.comschrieb im Newsbeitrag
news:11**********************@k78g2000cwa.googlegr oups.com...
>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 <ncould be DataTableRow <i>.
I am looking for a way to identify the DataGridViewRow showing my
DataTableRow.

Regards
Rainer

Feb 12 '07 #4

P: n/a
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[i].DataBoundItem as
DataRowView;
for (int j = 0; j < dr_arr.Length; j++)
{
if (drv.Row.Equals(dr_arr[j]))
{
this.dataGridView1.Rows[i].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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Feb 13 '07 #5

P: n/a
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]"" <je***@online.microsoft.comschrieb im Newsbeitrag
news:1v**************@TK2MSFTNGHUB02.phx.gbl...
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[i].DataBoundItem as
DataRowView;
for (int j = 0; j < dr_arr.Length; j++)
{
if (drv.Row.Equals(dr_arr[j]))
{
this.dataGridView1.Rows[i].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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.


Feb 13 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.