由于到期日期而更改gridview列颜色

时间:2014-02-07 02:29:43

标签: c# asp.net gridview

我正在尝试更改gridview上的过期日期列。我希望在我超过当前日期时或在当前日期的15天内更改颜色为红色,如果从当前数据日期开始15至30天将颜色设置为黄色。当我尝试在设置datetime变量时尝试实现rowdatabound时,我缺少gridview中的行,并且不显示颜色。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" OnDataBound="GridView1_DataBound" OnRowDataBound="GridView1_RowDataBound1">
    <Columns>
        <asp:BoundField DataField="MLSId" HeaderText="MLSId" SortExpression="MLSId" />
        <asp:BoundField DataField="Agent_FName" HeaderText="First Name" SortExpression="Agent_FName" />
        <asp:BoundField DataField="Agent_LName" HeaderText="Last Name" SortExpression="Agent_LName" />
        <asp:BoundField DataField="License_Num" HeaderText="License #" SortExpression="License_Num" />
        <asp:TemplateField HeaderText="License Exp" SortExpression="License_Exp">
             <EditItemTemplate>
                 <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("License_Exp") %>'></asp:TextBox>
             </EditItemTemplate>
             <ItemTemplate>
                 <asp:Label ID="Label1" runat="server" Text='<%# Bind("License_Exp", "{0:MM/dd/yyyy}") %>'></asp:Label>
             </ItemTemplate>
        </asp:TemplateField>
   </Columns>
</asp:GridView>

代码背后,

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    for (int i = 0; i <= GridView1.Rows.Count-1; i++)
    {
        DateTime lblDate = Convert.ToDateTime(GridView1.Rows[i].FindControl("Label1"));

        if (lblDate <= DateTime.Now.AddDays(15))
        {
            GridView1.Rows[i].Cells[4].BackColor = Color.Red;
        }
        else if (lblDate >= DateTime.Now.AddDays(16) && lblDate <= DateTime.Now.AddDays(30))
        {
            GridView1.Rows[i].Cells[4].BackColor = Color.Yellow;
        }
        else
        {
            GridView1.Rows[i].Cells[4].BackColor = Color.Blue;
        }
    }
}

2 个答案:

答案 0 :(得分:3)

for内部您不需要GridView1_RowDataBound1阻止,这是您应该做的事情:

protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string dateText = ((Label)e.Row.FindControl("Label1")).Text;

        if (!string.IsNullOrEmpty(dateText))
        {
            DateTime dateValue = DateTime.ParseExact(dateText, "MM/dd/yyyy", null);

            if (dateValue <= DateTime.Now.AddDays(15))
            {
                e.Row.Cells[4].BackColor = Color.Red;
            }
            else if (dateValue >= DateTime.Now.AddDays(16) && dateValue <= DateTime.Now.AddDays(30))
            {
                e.Row.Cells[4].BackColor = Color.Yellow;
            }
            else
            {
                e.Row.Cells[4].BackColor = Color.Blue;
            }
        }
        else
        {
            e.Row.Cells[4].BackColor = Color.Blue;
        }
    }
}

答案 1 :(得分:1)

每行都会引发

RowDataBound事件,您可以使用e.Row获取行

您可以更改以下代码。确保找到标签并将其转换为日期时间,没有任何错误。

 protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
  {
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label a = e.Row.FindControl("Label1") as Label;
        if (a != null)
        {
            DateTime lblDate;
            if(!DateTime.TryParse(a.Text, out lblDate)
            {
                // date time conversion not success 
                // you may have empty or invalid datetime 
                // do something in this case 
                return;
            }
            if (lblDate <= DateTime.Now.AddDays(15))
            {
                e.Row.Cells[4].BackColor = Color.Red;

            }
            else if (lblDate >= DateTime.Now.AddDays(16) && lblDate <= DateTime.Now.AddDays(30))
            {
                e.Row.Cells[4].BackColor = Color.Yellow;

            }
            else
            {
                e.Row.Cells[4].BackColor = Color.Blue;
            }

        }
    }
}