Click here to Skip to main content
14,697,262 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to select a selected cell row in a datagridview, the selected cell has color beige, code to color beige cell :
Dim dt As New Date
           For Each row As DataGridViewRow In DataGridView1.Rows

               For c As Int32 = 1 To DataGridView1.ColumnCount - 1
                   If row.Cells(c).Value IsNot DBNull.Value Then
                   dt = Date.Parse(row.Cells(c).Value)
                   If (Date.Today >= (dt.AddMonths(-1))) And (Date.Today <= dt) Then
                    row.Cells(c).Style.BackColor = Color.Beige
                    End If
                   End If

this code makes the cell that its date matches the routine to become bc.beige and works. Now I am trying to make the row(s) that have cell with color beige to be selected add them to a datatable and st the datatable as source of a new datagridview, I don't know how to make the cell's row to be selectedm this code is not working :
For h As Integer = 0 To DataGridView1.Rows.Count - 1
          For d As Int32 = 1 To DataGridView1.ColumnCount - 1
            If DataGridView1.Rows(h).Cells(d).Style.BackColor = Color.Beige Then
              DataGridView1.Rows(h).Selected = True
              MessageBox.Show("rows " + h + " added", "rows")
              End If

Any help thank you.
Updated 11-May-14 8:25am
CHill60 11-May-14 13:14pm
Why are you stepping through the rows and columns of DataGridView2 ... surely there won't be an exact match to DataGridView1?
Nelek 11-May-14 13:44pm
OP told, you were right, he had entered the 2nd datagrid and thanked you (just in case the non-solutions get nuked)
CHill60 11-May-14 16:59pm
Ah thank you! They have been nuked so I'll post my comment as the solution.

1 solution

As per my comment - I questioned why OP was stepping through the rows and columns of DataGridView2 instead of DataGridView1

OP confirmed that this was the solution.
Thanks to Nelek for letting me know

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