根据列值使DataGridView行成为一定的颜色 [英] Making DataGridView rows a certain color based on a column value

查看:155
本文介绍了根据列值使DataGridView行成为一定的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用vb.net,我有数据进入我的DGV,如果它是一个'1',我有一个标记为部署的列,我想在部署的列中有所有列为'1',如果是一个'0',我想让所有的行都是绿色的。这是我的方法,现在列是第10列,但它不喜欢=运算符。即使我使用1的字符串的equals比较运算符的引号。应该是一个整数,但我正在尝试的方式,我可以看到为什么它不工作。

  Private Sub LaptopGrid_CellFormatting(ByVal Sender As对象,ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs)处理LaptopGrid.CellFormatting 
对于i As Integer = 0到LaptopGrid.Rows.Count - 1
如果LaptopGrid.Rows(i).Cells(9 ).Value = 1然后
LaptopGrid.RowsDefaultCellStyle.BackColor = Color.Green
End If
Next
End Sub


解决方案

您的代码有几个问题。



首先,您正在处理 CellFormatting 事件,但您正在迭代每行以设置背景颜色。该事件是为了对某个单一的特定单元进行某些操作,在相关事件中指出的是: e.RowIndex e.ColumnIndex 。使用循环,您的行为比所需要的更多行,一遍又一遍地执行。



其次,VB有数据类型。 Int32 是一种类型, String 是另一种类型,而 Object 是另一个在比较之前,您需要将一种类型转换为其他类型。 LaptopGrid.Rows(r).Cells(c).Value 返回 Object (因为一个单元格可以保留任何东西) ,所以要比较 1 ,需要将其转换为整数。



最后,您可能不希望 CellFormatting 事件。如果有问题的单元格不在屏幕上,则事件不会触发(也许用户调整了列)。 RowPrePaint 另一方面将在行滚动到视图中触发。

  Private Sub dgv1_RowPrePaint(sender As Object,$ b $ as As DataGridViewRowPrePaintEventArgs)处理dgv1.RowPrePaint 

'不要做NewRow
如果e.RowIndex< 0 OrElse dgv1.Rows(e.RowIndex).IsNewRow然后返回

'转换为int32,然后比较
'只对此行 - e.RowIndex
如果转换。 ToInt32(dgv1.Rows(e.RowIndex).Cells(3).Value)> 3然后
dgv1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LemonChiffon
Else
dgv1.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.MistyRose
End If

End Sub



如果用户可以编辑该单元格值,则需要相应更新背景颜色。


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

解决方案

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

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

这篇关于根据列值使DataGridView行成为一定的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆