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

on Mar 07, 2013 11:16 PM

In my 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


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
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" "">
<html xmlns="">
<head id="Head1" runat="server">
      <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;
    <form id="form1" runat="server">
      <asp:GridView ID="GridView1" CssClass = "Grid" runat="server" AutoGenerateColumns="false" OnRowDataBound = "OnRowDataBound">
            <asp:BoundField DataField="Name" HeaderText="Name"/>
            <asp:BoundField DataField="DOB" HeaderText="Date Of Birth"/>

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
        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
        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