Hi Indradeo,
Please refer below sample. I have created that sameple using database provided from your side and sample code working fine.
SQL
CREATE TABLE [comp_box] (
[COMP_ID] INT IDENTITY (1, 1) NOT NULL,
[EMP_ID] NVARCHAR (10) NULL,
[COMP_MESSAGE] NVARCHAR (MAX) NULL,
[Comp_type] VARCHAR (20) NULL,
[STATUS_ID] NVARCHAR (10) DEFAULT ('01') NOT NULL,
[COMP_LODGE_DATE] DATE DEFAULT (getdate()) NULL,
[CLOSING_DATE] DATE NULL,
[REMARKS] VARCHAR (255) NULL,
[S_GUID] UNIQUEIDENTIFIER NULL,
[remote_ip] NVARCHAR (50) NULL,
[who] NVARCHAR (50) NULL,
[user_feedback] NVARCHAR (255) NULL,
[type] NVARCHAR (255) NULL,
[ATTEND_DATE] DATETIME NULL,
[ATTEND_BY] VARCHAR (255) NULL,
[Image] VARCHAR (50) NULL,
[remote_A_ip] NVARCHAR (50) NULL,
CONSTRAINT [PK_comp_box] PRIMARY KEY CLUSTERED ([COMP_ID] ASC)
);
INSERT INTO [comp_box]
VALUES ('101847', 'Application -:- Userid/Password Problem :- Employee no 101846 not working.','IT COMPLAINT', 3, '2/26/2022', NULL, NULL, NULL, NULL, '', NULL, NULL, '', '', '', NULL)
HTML
<asp:GridView ID="gvDetails" CssClass="Grid" runat="server" AutoGenerateColumns="false"
OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="COMP_ID" HeaderText="COMP_ID" />
<asp:BoundField DataField="EMP_ID" HeaderText="EMP_ID" />
<asp:BoundField DataField="COMP_MESSAGE" HeaderText="COMP_MESSAGE" />
<asp:BoundField DataField="STATUS_ID" HeaderText="STATUS_ID" />
<asp:TemplateField HeaderText="COMP_LODGE_DATE">
<ItemTemplate>
<asp:Label ID="lblDate" Text='<%# Eval("COMP_LODGE_DATE") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CLOSING_DATE" HeaderText="CLOSING_DATE" />
<asp:BoundField DataField="REMARKS" HeaderText="REMARKS" />
<asp:BoundField DataField="S_GUID" HeaderText="S_GUID" />
<asp:BoundField DataField="remote_ip" HeaderText="remote_ip" />
<asp:BoundField DataField="who" HeaderText="who" />
<asp:BoundField DataField="user_feedback" HeaderText="user_feedback" />
<asp:BoundField DataField="type" HeaderText="type" />
<asp:BoundField DataField="ATTEND_DATE" HeaderText="ATTEND_DATE" />
<asp:BoundField DataField="ATTEND_BY" HeaderText="ATTEND_BY" />
<asp:BoundField DataField="Image" HeaderText="Image" />
<asp:BoundField DataField="remote_A_ip" HeaderText="remote_A_ip" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.BindGrid();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime dateTime = Convert.ToDateTime((e.Row.FindControl("lblDate") as Label).Text);
if (dateTime.Subtract(DateTime.Now).TotalHours < 12)
{
e.Row.BackColor = Color.Green;
}
if (dateTime.Subtract(DateTime.Now).TotalHours > 24)
{
e.Row.BackColor = Color.Red;
}
}
}
private void BindGrid()
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlCommand cmd = new SqlCommand("SELECT COMP_ID, EMP_ID, COMP_MESSAGE ,Comp_type, STATUS_ID, COMP_LODGE_DATE, CLOSING_DATE, REMARKS, S_GUID" +
",remote_ip, who,user_feedback, type,ATTEND_DATE ,ATTEND_BY, Image, remote_A_ip FROM comp_box", con))
{
cmd.CommandType = CommandType.Text;
con.Open();
this.gvDetails.DataSource = cmd.ExecuteReader();
this.gvDetails.DataBind();
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dateTime As DateTime = Convert.ToDateTime((TryCast(e.Row.FindControl("lblDate"), Label)).Text)
If dateTime.Subtract(DateTime.Now).TotalHours < 12 Then
e.Row.BackColor = Color.Green
End If
If dateTime.Subtract(DateTime.Now).TotalHours > 24 Then
e.Row.BackColor = Color.Red
End If
End If
End Sub
Private Sub BindGrid()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using cmd As SqlCommand = New SqlCommand("SELECT COMP_ID, EMP_ID, COMP_MESSAGE ,Comp_type, STATUS_ID, COMP_LODGE_DATE, CLOSING_DATE, REMARKS, S_GUID" & ",remote_ip, who,user_feedback, type,ATTEND_DATE ,ATTEND_BY, Image, remote_A_ip FROM comp_box", con)
cmd.CommandType = CommandType.Text
con.Open()
Me.gvDetails.DataSource = cmd.ExecuteReader()
Me.gvDetails.DataBind()
con.Close()
End Using
End Using
End Sub
Screenshot