Change GridView Row Color Based on Date in BoundField in ASP.Net

baijuep
 
on Mar 07, 2013 11:16 PM
5887 Views

In my asp.net+vb we+access DB. i have a field named DOB which is date of birth. i displays the data in grid view whose DOB+90 is less than ot equl to current date

DOB+90<=now()

and it works currect. but when i try to used vb code to change the row colour as per data it works for this one.

 

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            If (e.Row.DataItem("DOR") <= Now()) Then
                e.Row.ForeColor = Drawing.Color.Red
            End If

But i want three diffrent conditions

1.  whose DOB=current month

2.  whose DOB comes in next month

3. whose DOB comes in month after next month

The font colour should be diffrent

how can i do that

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mustafa
 
on Mar 11, 2013 02:48 AM

Try this

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="VB.aspx.vb" Inherits="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
      <style type="text/css">
        .Grid td
        {
            background-color: #eee;
            color: black;
            font-family: Arial;
            font-size: 10pt;
            line-height: 200%;
            cursor: pointer;
            width: 100px;
        }
        .Grid th
        {
            background-color: #6C6C6C;
            color: White;
            font-family: Arial;
            font-size: 10pt;
            line-height: 200%;
            width: 100px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
      <asp:GridView ID="GridView1" CssClass = "Grid" runat="server" AutoGenerateColumns="false" OnRowDataBound = "OnRowDataBound">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="Name"/>
            <asp:BoundField DataField="DOB" HeaderText="Date Of Birth"/>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>

The above is the html code in which i have take two columns in my GridView

Imports System.Data
Imports System.Drawing

Partial Class VB
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim dt As New DataTable()
            dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("DOB")})
            dt.Rows.Add("Raj", "03/01/2013")
            dt.Rows.Add("Ravi", "05/12/2013")
            dt.Rows.Add("Ram", "04/16/2013")
            dt.Rows.Add("Ashok", "05/15/2013")
            dt.Rows.Add("Ajay", "05/25/2013")
            GridView1.DataSource = dt
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim dob As Date = e.Row.Cells(1).Text

            For Each cell As TableCell In e.Row.Cells
                If dob.Month = DateTime.Now.Month Then
                    cell.BackColor = Color.Red
                End If
                If dob.Month = DateTime.Now.Month + 1 Then
                    cell.BackColor = Color.Yellow
                End If
                If dob.Month = DateTime.Now.Month + 2 Then
                    cell.BackColor = Color.Orange
                End If
            Next
        End If
    End Sub
End Class

The above is the code where i have hardcodedly passed the data to my gridview and on the row_bound event i have compared the month from the gridview Date with the month of the current date