Hi ramco1917,
You need to use GridView RowDataBound event. In RowDataBound event you need to check the status column value and set the Literal by subtracting the Day.
Please refer below sample.
Note: For this sample i have used temporary DataTable. For more details refer Dynamically create DataTable and bind to GridView in ASP.Net.
HTML
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:TemplateField HeaderText="SessionDate">
<ItemTemplate>
<asp:Literal ID="ltrSessionDate" runat="server" Text='<%# Eval("SessionDate","{0: yyyy-MM-dd}") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindGrid();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string status = e.Row.Cells[0].Text.Trim();
Literal ltrSessionDate = e.Row.FindControl("ltrSessionDate") as Literal;
if (status.ToLower() == "approved")
{
ltrSessionDate.Text = Convert.ToDateTime(ltrSessionDate.Text).AddDays(-7).ToString("yyyy-MM-dd");
}
}
}
protected void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[2] {
new DataColumn("Status"),
new DataColumn("SessionDate")});
dt.Rows.Add("Approved", "2022-06-27");
dt.Rows.Add("Unapproved", "2022-07-03");
gvDetails.DataSource = dt;
gvDetails.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.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 status As String = e.Row.Cells(0).Text.Trim()
Dim ltrSessionDate As Literal = TryCast(e.Row.FindControl("ltrSessionDate"), Literal)
If status.ToLower() = "approved" Then
ltrSessionDate.Text = Convert.ToDateTime(ltrSessionDate.Text).AddDays(-7).ToString("yyyy-MM-dd")
End If
End If
End Sub
Protected Sub BindGrid()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Status"), New DataColumn("SessionDate")})
dt.Rows.Add("Approved", "2022-06-27")
dt.Rows.Add("Unapproved", "2022-07-03")
gvDetails.DataSource = dt
gvDetails.DataBind()
End Sub
Screenshot