Reputation: 542
I have a gridview that is getting data from a stored procedure. Column A is a date field. Column B has the text active (for all rows). However, I would like to change the text of 'active' (in the status column) to 'expired' based on the date - so if it's today's date or older, show expired. I know I could execute a job on the SQL server to change the column before importing into my table. However, I would like to only change the text and not alter the DB if possible. So, how would I manipulate the active column to show expired based on today's date.
con.Open();
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
tblMytable.DataSource = dt;
tblMytable.DataBind();
con.Close();
}
The webform:
<asp:GridView ID="tblMytable" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="DateField" HeaderText="Date" />
<asp:BoundField DataField="Status" HeaderText="Status" />
</Columns>
</asp:GridView>
Upvotes: 1
Views: 180
Reputation: 35524
If you make it a TemplateField you have much more control over the data and you can then use a Ternary operator to apply some logic.
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<%# Convert.ToDateTime(Eval("DateField")) < DateTime.Now ? "Expired" : "Active" %>
</ItemTemplate>
</asp:TemplateField>
Upvotes: 1