Displaying New ,Updated in gridview

Sandhyab
Posted by Sandhyab under ASP.NET category on | Points: 40 | Views : 898
It is an example to display new or update record by comparing the date stored in DB. If If the record inserted today it will display new in gridview which represnts newly added record.

 <asp:GridView ID="GridView1" runat="server" Width="100%" AutoGenerateColumns="false"  OnRowDataBound="grd_OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="LatestPost">
<ItemTemplate>
<asp:HyperLink ID="Hyper_LatestPost" runat="server" Text='<%# Eval("PostTitle")%>'
NavigateUrl='<%# WriteUrl(Eval("PostTitle").ToString(), Eval("PostId").ToString()) %>'></asp:HyperLink>
<asp:Label ID="lblStatus" runat="server"></asp:Label>
<asp:Label ID="lblStatusUpdate" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="category">
<ItemTemplate>
<asp:Label ID="label3" runat="server" Text='<%# Eval("Category")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Posted Date">
<ItemTemplate>
<asp:Label ID="label4" runat="server" Text='<%# Eval("Date")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>



Code Behind:

 protected void grd_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblDate = (Label)e.Row.FindControl("label4");
Label lblStatus = (Label)e.Row.FindControl("lblStatus");
Label lblStatusUpdate = (Label)e.Row.FindControl("lblStatusUpdate");
string date = DateTime.Today.ToString();
if (lblDate.Text == date)
{
lblStatusUpdate.Text = "New";
lblStatusUpdate.ForeColor = System.Drawing.Color.Red;
}
else lblStatus.Text = "Update";
lblStatus.ForeColor = System.Drawing.Color.Green;

}

}


Comments or Responses

Login to post response