Reputation: 119
Hi i am a little bit confused my code not getting triggered on another tab. I have a tabcontrol and have a 3 Tabs on it and have a datagridview for each tab. Datagridview1,2 and 3 for each tab.
In datagridview1 i have this code. This code will execute on datagridview1.cellclick.
Dim i As Integer
Dim j As Integer
For i = 0 To 50
For j = 0 To 50
If DataGridView3.Rows(i).Cells(1).Value = DataGridView2.Rows(j).Cells(0).Value Then
DataGridView3.Rows(i).DefaultCellStyle.BackColor = Color.DarkSlateGray
End If
Next
Next
If my datagridview3 is on Tabpage3 this code is not working but if i place my datagridview3 on tabpage1 the code is working properly my selected row will be color gray. Am i doing it wrong ??
Upvotes: 0
Views: 71
Reputation: 827
Try the RowPrePaint Event.
Private Sub DataGridView1_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
If e.RowIndex <= 50 Then
Dim DgvRow As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
For i As Integer = 0 To 50
If DgvRow.Cells(1).Value = DataGridView1.Rows(i).Cells(0).Value Then
DgvRow.DefaultCellStyle.BackColor = Color.DarkSlateGray
Else
DgvRow.DefaultCellStyle.BackColor = Color.Empty
End If
Next
End If
End Sub
How I would handle this situation on rows less than say 2000 is create a bindingsource to the datasource for datagridview2 and use the Find method.
IE:
'Declare a new bindingsource at Class scope
'Set its datasource to datatable used for DGV2
Dt2BindSource.DataSource = DtSet.Tables(1)
'Set bindingsource for DGV2 to bindingsource
DataGridView2.DataSource = Dt2BindSource
Private Sub DataGridView1_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles DataGridView1.RowPrePaint
Dim DgvRow As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
Dim idx As Integer = Dt2BindSource.Find("Code", DgvRow.Cells("SubjectCode").Value.ToString)
If idx >= 0 Then
'Code exists
DgvRow.DefaultCellStyle.BackColor = Color.DarkSlateGray
Else
'Code no exist
DgvRow.DefaultCellStyle.BackColor = Color.Empty
End If
End Sub
Upvotes: 1