如果日期小于其他列单元格,如何显示gridview单元格的颜色
问题描述:
大家好,
我想在asp.net gridview单元格中显示背景颜色。
例如:
我有两个名为DateFrom和DateTo的列。
Hi everyone,
I want to display background color in in asp.net gridview cell.
For example:
I have two columns with name DateFrom and DateTo.
DateFrom(2nd column in GridView)
DateTo(3rd Column in GridView)
如果datefrom小于Date,则应为红色。
如果DateTo大于DateFrom,它应该是是绿色
if datefrom is less than Dateto it should be red.
and if DateTo is greater than DateFrom it should be GREEN
item.Cells[1].BackColor = Color.FromName("#FF6A6A")RED color;
item.Cells[2].BackColor = Color.FromName("#77FF77")Green Color;
请帮助,谢谢。
Please help, Thanks.
答
你需要使用GridView的RowDataBound
,例如:
You need to useRowDataBound
of GridView, something like:
protected void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
DataControlRowType rtype = e.Row.RowType;
if (rtype == DataControlRowType.DataRow && rtype != DataControlRowType.Footer
&& rtype != DataControlRowType.Separator && rtype != DataControlRowType.Header
&& rtype != DataControlRowType.Pager)
{
// Control specific
TextBox tb1 = (TextBox)e.Row.FindControl("myDate1");
TextBox tb2 = (TextBox)e.Row.FindControl("myDate2");
// Your logic for data of tb1 & tb2.
// Your setting of the color for defined column.
}
}