locked
Highlight some rows in vb.net 2.0 gridview

    Question

  • Hello everyone,

    I have a gridview and everything is loaded through dataset. I am able to search as well which is what i want. There is one column called Expires which has dates on when it expires, some of the has already expired and others will expire in a few months. What I want to do is to highlight the rows that have expired in red colour and highlight the rows that are about to expire maybe 1 or 2 month to expiry in yellow.

    Please note the program is made under Framework 2.0. Thanks

    I would appreciate for any help

    Thanks

    Jagar,
    Thursday, September 18, 2008 7:52 PM

Answers

  •  Hi jagarm
    Jagarm said:

    Hello everyone,

    I have a gridview and everything is loaded through dataset. I am able to search as well which is what i want. There is one column called Expires which has dates on when it expires, some of the has already expired and others will expire in a few months. What I want to do is to highlight the rows that have expired in red colour and highlight the rows that are about to expire maybe 1 or 2 month to expiry in yellow.

    Please note the program is made under Framework 2.0. Thanks

    I would appreciate for any help

    Thanks

    Jagar,

    You could use the following code, it wroks fine on my computer,you could set the" Expires_data" as flag and change the color of dgr.DefaultCellStyle.BackColor.

    Public Class Form1  
        Dim dt As DataTable = New DataTable()  
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
            dt.Columns.Add("name")  
     
            dt.Columns.Add("Expires_data")  
     
            dt.Columns.Add("Expires_flag")  
     
            dt.Rows.Add("aaaa1", "10", 0)  
     
            dt.Rows.Add("aaaa2", "10", 0)  
     
            dt.Rows.Add("aaaa3", "50", 1)  
     
            dt.Rows.Add("aaaa4", "4", 0)  
     
            dt.Rows.Add("aaaa5", "30", 3)  
     
            dt.Rows.Add("baaaaa6", "5", 0)  
     
            dt.Rows.Add("baaaaa7", "13", 0)  
     
            dt.Rows.Add("baaaaa8", "12", 0)  
     
            dt.Rows.Add("baaaaa9", "4", 0)  
     
     
     
            Me.DataGridView1.DataSource = dt 
     
            Me.DataGridView1.Columns(2).Visible = False 'make the Expires column invisible  
     
     
     
     
            Dim dgr As DataGridViewRow  
            For Each dgr In Me.DataGridView1.Rows  
     
     
                If Not dgr.IsNewRow Then  
     
                    If dgr.Cells("Expires_flag").Value.ToString() = "" Then  
     
                        dgr.Cells("Expires_flag").Value = 1 
                    End If  
     
                    Dim Expires As Integer = CType(dgr.Cells("Expires_flag").Value, Integer)  
     
                    If Expires = 1 Then  
     
                        dgr.DefaultCellStyle.BackColor = Color.Red  
     
                    ElseIf Expires = 2 Then  
     
                        dgr.DefaultCellStyle.BackColor = Color.Blue  
                    ElseIf Expires = 3 Then  
     
                        dgr.DefaultCellStyle.BackColor = Color.Yellow  
                    Else  
     
                        dgr.DefaultCellStyle.BackColor = Color.White  
                    End If  
     
                End If  
     
            Next  
     
     
        End Sub  
     
    End Class 


    if you have any issue, please feel free to tell us.
    best wish
    xingwei



    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Edited by Xingwei Hu Wednesday, September 24, 2008 10:09 AM adding
    • Marked as answer by Xingwei Hu Thursday, September 25, 2008 3:47 AM
    Wednesday, September 24, 2008 9:58 AM