Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

.net - Making DataGridView rows a certain color based on a column value

I am using vb.net and I have data coming into my DGV and I have a column labeled deployed if it's a '1', I want to have all the rows with '1' in the deployed column RED and if it's a '0', I want all the rows to be GREEN. This is my method I have, right now the column is the 10th column but it doesn't like the = operator. Even when I use quotes on the 1 with equals compare operator for strings. Should be an integer but I was trying every way I could to see why it's not working.

Private Sub LaptopGrid_CellFormatting(ByVal Sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles LaptopGrid.CellFormatting
    For i As Integer = 0 To LaptopGrid.Rows.Count - 1
        If LaptopGrid.Rows(i).Cells(9).Value = 1 Then
            LaptopGrid.RowsDefaultCellStyle.BackColor = Color.Green
        End If
    Next
End Sub
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are a couple of issues with your code.

First, you are handling the CellFormatting event but you are iterating every row to set the backcolor. That event is meant for you to do something to a single, specific cell, the one in question is indicated in the event args: e.RowIndex and e.ColumnIndex. Using a loop, you are acting on many more rows than needed and doing so over and over.

Second, VB has data types. Int32 is one type, String is another, and Object is yet another. You need to convert one type to the other type before you compare. LaptopGrid.Rows(r).Cells(c).Value returns Object (since a cell can literally hold anything), so to compare to 1 you need to convert it to integer.

Finally, you may not want the CellFormatting event for this. If the cell in question is not on screen, the event wont fire (perhaps the user resized the columns). RowPrePaint on the other hand will fire when the row scrolls into view.

Private Sub dgv1_RowPrePaint(sender As Object, 
                   e As DataGridViewRowPrePaintEventArgs) Handles dgv1.RowPrePaint

    ' dont do the NewRow
    If e.RowIndex < 0 OrElse dgv1.Rows(e.RowIndex).IsNewRow Then Return

    ' convert to int32, then compare
    ' act on just this row - e.RowIndex
    If Convert.ToInt32(dgv1.Rows(e.RowIndex).Cells(3).Value) > 3 Then
        dgv1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LemonChiffon
    Else
        dgv1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.MistyRose
    End If

End Sub

enter image description here

If the user can edit that cell value, you will want to update the back color accordingly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...