Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change data grid view row colour depend upon the value

Tags:

vb.net

I have a DataGridView like this: enter image description here

I am binding the DataGridView like this:

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.

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

like image 774
user2603688 Avatar asked Nov 19 '25 13:11

user2603688


2 Answers

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.

like image 200
Mayur Borad Avatar answered Nov 21 '25 08:11

Mayur Borad


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
like image 23
huMpty duMpty Avatar answered Nov 21 '25 10:11

huMpty duMpty