DataGridView - color causes error

0
0

I am trying to set the background color on a DataGridView based off a property in the bound object.  The color is set, but when there are more than 1 rows in the DGV, I get an error, but not in the debugger, just as a popup on screen.  The error is because of a null value.  Thing is, each row is in the datagrid view because it does exist.

I am using dgvQueueHistory.DataSource = hwlist

So each row has a row.DataBoundItem, and isn’t null.

If I could tell what line was causing the problem, I would report it, but like I said, it doesn’t show up in VS2015 while debugging, only after everything is “drawn” to the screen, then the error appears.

I downloaded and installed the latest version this afternoon (3/3/2017)

 

Private Sub dgvQueueHistory_Paint(sender As Object, e As Wisej.Web.PaintEventArgs) Handles dgvQueueHistory.Paint

For Each row In dgvQueueHistory.Rows
Dim tag As hdBO.HW.Monitor.QueueHistory = TryCast(row.DataBoundItem, hdBO.HW.Monitor.QueueHistory)
If IsNothing(tag) = False Then
Select Case tag.RowDisplayColor
Case “Red”
row.DefaultCellStyle.BackColor = Drawing.Color.Red
Case “Yellow”
row.DefaultCellStyle.BackColor = Drawing.Color.Yellow
Case “Green”
row.DefaultCellStyle.BackColor = Drawing.Color.Green
End Select
End If
Next
End Sub

  • You must to post comments
0
0

Here is the code I was using.

To bind the list(of…) to the datagrid was:

Me.dgvQueueHistory.DataSource = utl.QueueHistory

And to do the color

Private Sub dgvQueueHistory_Paint(sender As Object, e As Wisej.Web.PaintEventArgs) Handles dgvQueueHistory.Paint
For Each row In dgvQueueHistory.Rows
Dim tag As hdBO.HW.Monitor.QueueHistory = TryCast(row.DataBoundItem, hdBO.HW.Monitor.QueueHistory)
If IsNothing(tag) = False Then
Select Case tag.RowDisplayColor
Case “Red”
row.DefaultCellStyle.BackColor = Drawing.Color.Red
Case “Yellow”
row.DefaultCellStyle.BackColor = Drawing.Color.Yellow
Case “Green”
row.DefaultCellStyle.BackColor = Drawing.Color.Green
End Select
End If
Next
End Sub

 

I am now using the the DataBindingComplete event and it appears to work better.

I am still getting an error after a time, the “Invalid Session” error that I reported in another post.

Thanks for the help.

Shawn

  • You must to post comments
0
0

I just tried to update the row colors and it’s working well. See below. The correct event to detect when the rows are created in a data-bound grid is DataBindingComplete. There are other events for rows added programmatically. The Paint event is an image callback and changing the style causes the grid to “push” updates to the client up to a threshold after which it simply issues a new data reload from the client.

 

private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
foreach (var row in this.dataGridView1.Rows)
{
row.DefaultCellStyle.BackColor = Color.Red;
}
}

  • You must to post comments
0
0

The Paint event is not related to the rendering of the rows. It’s to paint into the graphics object that represents the background image of the control. For a DGV it doesn’t make much sense to paint the whole grid. I don’t know what happens if the code manipulates the row in a Paint callback – which is just an image request.

You should manage the rows outside of the Paint event.

Can you send a code snippet that shows how you bound the color to the data source?

  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.