vb.net 更改数据网格视图行颜色取决于值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17858002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:02:15  来源:igfitidea点击:

change data grid view row colour depend upon the value

vb.net

提问by user2603688

I have a DataGridView like this: enter image description here

我有一个像这样的 DataGridView: 在此处输入图片说明

I am binding the DataGridView like this:

我像这样绑定 DataGridView:

Dim cmd As New SqlCommand("DashBordFetch", con.connect)  
cmd.CommandType = CommandType.StoredProcedure   
cmd.Parameters.Add("@locid", SqlDbType.Int).Value = locid
da.SelectCommand = cmd
da.Fill(ds)
DGVDashBoard.DataSource = ds.Tables(0)

I want to my DataGridView to have a red row color when the Value is 1. How can I do that? I am working in VB.NET.

当 Value 为 1 时,我希望我的 DataGridView 具有红色行颜色。我该怎么做?我在 VB.NET 工作。

I have tried the code in one of the answers provided, it looks like this:

我在提供的答案之一中尝试了代码,它看起来像这样:

回答by Mayur Borad

Try this

尝试这个

 Private Sub DGVDashBoard_CellFormatting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DGVDashBoard.CellFormatting
            Dim drv As DataRowView
            If e.RowIndex >= 0 Then
                ' Add Your condition here ignore following
                If e.RowIndex <= ds.Tables(0).Rows.Count - 1 Then
                    drv = ds.Tables(0).DefaultView.Item(e.RowIndex)
                    Dim c As Color
                    If drv.Item("Value").ToString = "1" Then
                        c = Color.Red                        
                    End If
                    e.CellStyle.BackColor = c
                End If
            End If
        End Sub

Let me know if this doesn't work.

如果这不起作用,请告诉我。

回答by huMpty duMpty

Sub CustomersGridView_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

    If e.Row.RowType = DataControlRowType.DataRow Then

      If(your condition)
         e.Row.BackColor = Drawing.Color.Red 
      End If

    End If

  End Sub

回答by Jason Wayne Wilson

Thanks Senthilkumar I was looking for something that would allow me to color the background of a row if the Text equals Closed. Here is my final solution I just had to add the ToString after value. The DataGridviewTextBoxColumn16 name is from the datagridview properties. Click the Arrow at the top right of DataGridView, edit columns and find the name value. Hope that helps someone.

谢谢 Senthilkumar 我正在寻找一些东西,如果文本等于关闭,我可以让我为行的背景着色。这是我的最终解决方案,我只需要在值之后添加 ToString。DataGridviewTextBoxColumn16 名称来自 datagridview 属性。单击 DataGridView 右上角的箭头,编辑列并找到名称值。希望能帮助某人。

For Each rw As DataGridViewRow In DataGridView1.Rows
        If rw.Cells("DataGridViewTextBoxColumn16").Value.ToString = "Closed" Then
            rw.DefaultCellStyle.BackColor = Color.Red
        Else
            rw.DefaultCellStyle.BackColor = Color.Green
        End If
    Next

回答by Yogesh Sankar

'Use this code for change color of specific row in cellformatting event of 'datagridview

'在'datagridview 的单元格格式事件中使用此代码更改特定行的颜色

        For Each drview As DataGridViewRow In dgrawPRoducts.Rows
            If drview.Cells.Count >= 14 Then
                If Not IsNothing(drview.Cells(14).Value) Then
                    If drview.Cells(14).Value.ToString.Trim = "N" Then
                        For i As Integer = 0 To drview.Cells.Count - 1
                            drview.Cells(i).Style.ForeColor = Color.Red
                           drview.Cells(i).Style.BackColor= Color.Gray
                        Next

                    End If
                End If
            End If

        Next

回答by AngelaG

here's my code:

这是我的代码:

For Each row As GridViewRow In GridView1.Rows
    row.BackColor = Drawing.Color.White
Next row

回答by sk2185

    For Each rw As DataGridViewRow In DataGridView1.Rows 
    If rw.Cells("slno").Value =1 Then 
    rw.DefaultCellStyle.BackColor = Color.Red
    Else 
    rw.DefaultCellStyle.BackColor = Color.Green
    End If 
    Next