根据过期日期更改网格视图列的颜色

3

我试图更改一个网格视图上的过期日期列。当过期日期早于当前日期或距离当前日期在15天内时,我想将其颜色改为红色;当距离当前日期在15到30天时,我想将其颜色改为黄色。当我尝试实现rowdatabound并设置datetime变量时,我发现丢失了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个回答

3

您不需要在GridView1_RowDataBound1中使用for块,您可以采取以下措施:

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;
        }
    }
}

当我遇到日期单元格的空值时,网格视图会停止而不是默认为蓝色并继续循环。如果我可以默认为蓝色或留空/白色并继续条件,那将是目标。我尝试使用for循环来继续处理空值,但这并不起作用。 - Matthew Halle
请查看我的修改后的答案,如果日期为空,则背景颜色将为蓝色。 - ekad

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;
            }

        }
    }
}

这个程序正常工作,直到日期的值为空。我尝试添加了一个else语句,但是它仍然无法填充Gridview超过空标签。只有在遇到空值之前遵循逻辑。 - Matthew Halle
如果Label1.Text的值为空,你想要做什么? - Damith
我试图让未输入/为空的单元格变为白色。 - Matthew Halle

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接