Hi kankon,
Use GridView RowDataBound event and check the cell value to set the color.
Check this example. Now please take its reference and correct your code.
I have binded GridView using temporary Datatable. You have to use your code for binding the GridView from database using stored procedure.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="OnRowEditing" OnRowDataBound="OnRowDataBound"
OnRowUpdating="OnRowUpdating" OnRowCancelingEdit="OnRowCancelingEdit" OnRowDeleting="OnRowDeleting">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:TemplateField HeaderText="Time">
<ItemTemplate>
<asp:Label ID="lblTime" runat="server" Text='<%# Eval("Time", "{0:hh:mm tt}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Time", typeof(DateTime)) });
dt.Rows.Add(1, "John Hammond", "2020-05-05 1:20 PM");
dt.Rows.Add(2, "Mudassar Khan", "2020-05-05 10:30 AM");
dt.Rows.Add(3, "Suzanne Mathews", "2020-05-05 9:23 AM");
dt.Rows.Add(4, "Robert Schidner", "2020-05-05 4:50 PM");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnRowCancelingEdit(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnRowDeleting(object sender, GridViewDeleteEventArgs e)
{
this.BindGrid();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowIndex != GridView1.EditIndex)
{
(e.Row.Cells[3].Controls[2] as LinkButton).Attributes["onclick"] = "return confirm('Do you want to delete this row?');";
DateTime time = Convert.ToDateTime((e.Row.FindControl("lblTime") as Label).Text);
if (time.ToString("tt").ToUpper() == "PM")
{
(e.Row.FindControl("lblTime") as Label).ForeColor = Color.Red;
}
if (time.ToString("tt").ToUpper() == "AM")
{
(e.Row.FindControl("lblTime") as Label).ForeColor = Color.Blue;
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
BindGrid()
End If
End Sub
Private Sub BindGrid() Handles Me.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"), New DataColumn("Name"), New DataColumn("Time", GetType(DateTime))})
dt.Rows.Add(1, "John Hammond", "2020-05-05 1:20 PM")
dt.Rows.Add(2, "Mudassar Khan", "2020-05-05 10:30 AM")
dt.Rows.Add(3, "Suzanne Mathews", "2020-05-05 9:23 AM")
dt.Rows.Add(4, "Robert Schidner", "2020-05-05 4:50 PM")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub OnRowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs)
GridView1.EditIndex = e.NewEditIndex
Me.BindGrid()
End Sub
Protected Sub OnRowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnRowCancelingEdit(ByVal sender As Object, ByVal e As EventArgs)
GridView1.EditIndex = -1
Me.BindGrid()
End Sub
Protected Sub OnRowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs)
Me.BindGrid()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow AndAlso e.Row.RowIndex <> GridView1.EditIndex Then
TryCast(e.Row.Cells(3).Controls(2), LinkButton).Attributes("onclick") = "return confirm('Do you want to delete this row?');"
Dim time As DateTime = Convert.ToDateTime((TryCast(e.Row.FindControl("lblTime"), Label)).Text)
If time.ToString("tt").ToUpper() = "PM" Then
TryCast(e.Row.FindControl("lblTime"), Label).ForeColor = Color.Red
End If
If time.ToString("tt").ToUpper() = "AM" Then
TryCast(e.Row.FindControl("lblTime"), Label).ForeColor = Color.Blue
End If
End If
End Sub
Screenshot