473,241 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Find DataRow in DataGridView

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
5 39564
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: wackyphill | last post by:
Anyone seen this? Know if its a bug? I'm pretty sure it is. I have a DataGridView control in a tab control who's datasource is a table in a dataset. In certain situations when I add a row to the...
1
by: sapkal.manish | last post by:
Question : How can I find actual row position of DataTable in DataGridView when DataTable is sorted / My source code like this Private WithEvent dgvInwDet as new DataGridView Private...
3
by: 蟲蟲小姐 | last post by:
I have two textboxes. When i click "ADD" button, I want the content of textboxes added to the DataGridView. But I don't know how to write the syntax in Visual Basic DOT NET. Anyone can help me?...
2
by: bob | last post by:
Can anyone tell me the best way to update a dataset while it is being edited/viewed in the DataGridView control? Is this something that should be inserted into one of the grid's events? or should...
1
by: Dranizz | last post by:
I want to set the value of a cell in a datagridview. DatagridView.CurrentRow.Cells(CEllName).value = "Blabla" When I do this in code and when it's the first cell I edit in a new row, it does...
4
by: havana7 | last post by:
Hi people, with this command I can get the row index of founded records: i = dv.Find(txtFind.Text); but is there a good method that lets me show only founded records in DataGridView? Or is it...
8
by: Kyote | last post by:
On my form I have a datagridview that's bound to a table on my DB. When I run the app then click on a column header to sort, it sorts just fine. But my problem is a result of that sort. Even...
0
by: Dennis Francek | last post by:
Hello I have populated a treeview from my dataset and by clicking in the treeview i want to get the corresponding table shown in a datagridview. Ive managed this by looking at each DataRow and...
5
by: TheSteph | last post by:
Hi ! I have a DataGridView with a Date (DateTime) Column. When a user edit the cell and change the date I woulk like to allow him to write "081501" and programmatically transform the entered...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.