[RESOLVED] evaluating a DGV Row and change row bavkground color
My DGV's last visible column in a boolean. I am needing to go through each row and determine if the value of dgv item bFlag is true then turn the row back ground red. Here is my start of it. It appears to be only making one loop then giving the message box. I would appreciate an extra set of eyes on this. Thnx
Code:
Private Sub EvalGrid()
BindDGV(dgvResults)
Dim cnt As Integer = dgvResults.Rows.Count
Try
For x As Integer = 0 To cnt - 1
'MessageBox.Show(CStr(x) + " of " + CStr(cnt))
Dim item As String '= Number.ToString()
item = dgvResults.Item("personid", x).Value
If dgvResults.Item("bFlag", x).Value = True Then
With dgvResults
'.Rows(x).Selected = True
.Item("personid", x).Style.BackColor = Color.Red
End With
End If
Next
Catch
End Try
Dim editCnt As Integer = dgvResults.SelectedRows.Count
Me.dgvResults.Focus()
MessageBox.Show("There are " & editCnt & " records that needs to be corrected")
End Sub
***************************************************
Smartacus comes packaged "As Is With No Warranty"
Re: evaluating a DGV Row and change row bavkground color
Use CellFormatting event to alter the background color, in your case when the column bFlag value is True as shown below.
Code:
Private Sub dgvResults_CellFormatting( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
Handles dgvResults.CellFormatting
If e.ColumnIndex = dgvResults.Columns("bFlag").Index Then
If CBool(e.Value) Then
e.CellStyle.BackColor = Color.Red
End If
End If
End Sub
Re: evaluating a DGV Row and change row bavkground color
Originally Posted by Smartacus
Thats cool. How do I affect the entire row?
Chiming in from my iPad so I can not provide examples. Use Google and search on DataGridView bands. Will be back tomorrow night if you still need examples as I have some.
Re: evaluating a DGV Row and change row bavkground color
Some success, but not there yet. The following code changes all bands True or False.
Code:
Private Sub dgvResults_CellFormatting( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
Handles dgvResults.CellFormatting
If e.ColumnIndex = dgvResults.Columns("bFlag").Index Then
For Each band As DataGridViewBand In dgvResults.Rows
If CBool(dgvResults.Rows(band.Index).Cells("Bflag").Value) = True Then
band.DefaultCellStyle.BackColor = Color.BlanchedAlmond
End If
Next
End If
End Sub
***************************************************
Smartacus comes packaged "As Is With No Warranty"
Re: evaluating a DGV Row and change row bavkground color
Here is an example of using Bands that I did for another post. The original post condition was to make specific cells bold which I left and added code to make an entire row background color red.
Re: evaluating a DGV Row and change row bavkground color
Thank you!
I liked your Font Bold section so I also used that.
Code:
Public Sub ConditionsColorPrep(ByVal sender As Windows.Forms.DataGridView)
For Row As Integer = 0 To sender.Rows.Count - 1
If Not sender.Rows(Row).IsNewRow Then
If sender.Rows(Row).Cells("Bflag").Value IsNot Nothing Then
If sender.Rows(Row).Cells("Bflag").Value = True Then
sender.Rows(Row).DefaultCellStyle.BackColor = Color.BlanchedAlmond
End If
End If
End If
Next
End Sub
***************************************************
Smartacus comes packaged "As Is With No Warranty"
Re: evaluating a DGV Row and change row bavkground color
Originally Posted by Smartacus
Thank you!
I liked your Font Bold section so I also used that.
Code:
Public Sub ConditionsColorPrep(ByVal sender As Windows.Forms.DataGridView)
For Row As Integer = 0 To sender.Rows.Count - 1
If Not sender.Rows(Row).IsNewRow Then
If sender.Rows(Row).Cells("Bflag").Value IsNot Nothing Then
If sender.Rows(Row).Cells("Bflag").Value = True Then
sender.Rows(Row).DefaultCellStyle.BackColor = Color.BlanchedAlmond
End If
End If
End If
Next
End Sub
Re: [RESOLVED] evaluating a DGV Row and change row bavkground color
Originally Posted by Smartacus
You actually did more than just solve this problem. You gave a clear example of how I can streamline my code by using "Sender" for better OO coding!
In regards to sender within the contexts of the module DataGridViewExtensions when I declare sender as shown below
Code:
Public Sub InvokeBanding(ByVal sender As Windows.Forms.DataGridView)
I prepend DataGridView with the namespace it comes from because this module was extracted from a DLL I created which by default does not have a reference to System.Windows.Forms.
So if you are always going to work with code such as in DataGridViewExtensions in a Windows Forms project you can remove the namespace i.e.
Code:
Public Sub InvokeBanding(ByVal sender As DataGridView)
While if the module might end up in a DLL keep the namespace as is now.