【问题标题】:Change gridview column color due to expiration date由于到期日期更改 gridview 列颜色
【发布时间】:2014-02-07 02:29:43
【问题描述】:

我正在尝试更改网格视图上的到期日期列。当我超过当前日期或在当前日期后 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;
        }
    }
}

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您不需要在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;
            }
        }
    }
    

    【讨论】:

    • 当我到达日期单元格的空值时,gridview 停止而不是默认为蓝色并继续循环。如果我可以默认为蓝色或保留空白/白色并继续其他可能值的条件将是目标。我正在尝试使用 for 循环继续越过 null 值,但这不起作用。
    • 查看我编辑的答案,如果日期为空,则背景颜色为蓝色
    【解决方案2】:

    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 但它仍然不会填充超过空标签的网格视图。只遵循逻辑直到空值。
    • Label1.Text 有空值怎么办?
    • 我试图让任何未输入/为空的单元格变为白色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2017-04-06
    • 2013-07-10
    相关资源
    最近更新 更多