I don't know if using Link is the best solution for your problem.
You should first try to simplify the code in DataGridView.CellFormatting
event. Some possibles ways:
- Create one private instance of regular and bold Font to avoid to create multiples instance in the method
- I guess you don't need to test the second condition if first is passed. So
If End If
+If End If
could be replaced toIf Else End If
(don't need to test the two expressions) - Use
If x AndAlso y Then
instead ofIf x And y Then
: if first expression is false, the second won't be tested I don't understand the purpose of this
If e.RowIndex <= DataSet1.Tables("dtArticles").Rows.Count - 1 Then
. If the goal is not test if the row is the NewRow (withAllowUserToAddRows
property set to True), simplier would be thisIf DataGridView1.Rows(e.RowIndex).IsNewRow then
Private _RegularFont As New Font("Microsoft Sans Sherif", 8, FontStyle.Regular)
Private _BoldFont As New Font("Microsoft Sans Sherif", 8, FontStyle.Bold)
Private Sub dgvArticles_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvArticles.CellFormatting
Dim drv As DataRowView
If e.RowIndex >= 0 Then
If Not dgvArticles.Rows(e.RowIndex).IsNewRow Then
drv = View.Item(e.RowIndex)
'Bolds if it is standard, makes it regular if it's not standard and already bold
If drv.Item("Standard").ToString = "Yes" Then
dgvArticles.Rows(e.RowIndex).DefaultCellStyle.Font = _BoldFont
Else 'First condition tested, test second
'Test if Standard is "No" and if the row is currently bold, if it is, put it back to regular
If drv.Item("Standard").ToString = "No" AndAlso Not dgvArticles.Rows(e.RowIndex).DefaultCellStyle.Font Is Nothing Then
If dgvArticles.Rows(e.RowIndex).DefaultCellStyle.Font.Bold Then
dgvArticles.Rows(e.RowIndex).DefaultCellStyle.Font = _RegularFont
End If
End If
End If
'Puts a red color to the rows who are not available
If drv.Item("Available").ToString = "No" Then
'Change back color
Dim c As Color
c = Color.LightSalmon
e.CellStyle.BackColor = c
End If
End If
End If
End Sub
Using link, you could do something like this (for your purpose, you should not iterate into DataView.Rows
to retrieve index, but directly in DataGridView.Rows
):
Dim _BoldFont As New Font("Microsoft Sans Sherif", 8, FontStyle.Regular)
Dim q = From r In Me.DataGridView1.Rows
Where CType(r, DataGridViewRow).Cells("Standard").Value.ToString = "Oui"
Select r
For Each item In q
CType(item, DataGridViewRow).DefaultCellStyle.Font = _BoldFont
Next
Dim q = From r In Me.DataGridView1.Rows
Where CType(r, DataGridViewRow).Cells("Available").Value.ToString = "Non"
Select r
For Each item In q
CType(item, DataGridViewRow).DefaultCellStyle.BackColor = Color.LightSalmon
Next