簡體   English   中英

根據條件動態更改GridView行的背景顏色

[英]Changing the Background color of GridView Row as per condition dynamically

我試圖根據gridview中的標簽值更改gridview行的顏色。 我在gridview中有結束日期值,所以如果結束日期<今天的日期,我想更改背景顏色。

    <asp:GridView ID="gv_profile" runat="server" AutoGenerateColumns="false" Width="300px" OnRowDataBound="OnRowDataBound" DataKeyNames="ID"  >

  <Columns>

    <asp:TemplateField ItemStyle-Width="10px" HeaderText="ID" >
     <ItemTemplate>
      <asp:Label ID="LblID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
       </ItemTemplate>
         </asp:TemplateField>


    <asp:TemplateField ItemStyle-Width="50px" HeaderText="End Date">
      <ItemTemplate>
      <asp:Label ID="lblEndDate" runat="server" Text='<%# Eval("End_Date", "{0: yyyy-MM-dd}") %>'></asp:Label>
       </ItemTemplate>
          </asp:TemplateField>

 </Columns>

  </asp:GridView>

背后的代碼

  protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
   {
        if (e.Row.RowType == DataControlRowType.DataRow)
         {

         }
     }

應該適合您的需求

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow && !((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit)))
    {
        Label lblEndDate = (Label)e.Row.FindControl("lblEndDate");
        DateTime EndDate = DateTime.Parse(lblEndDate.Text);
        if (EndDate<DateTime.Today)
        {
            e.Row.BackColor = System.Drawing.Color.MistyRose;
        }
    }

}

 protected void grdView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Attributes.Add("onmouseover", "MouseEvents(this, event)");
            e.Row.Cells[0].Attributes.Add("onmouseout", "MouseEvents(this, event)");


            if ((lblhidisinsert.Value == "1") )
            {
                e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#ecf8ec");
            }           
        }
    }
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int id = Convert.ToInt32(e.Row.Cells[1].Text);
        if (id > 0)
        {
            e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
        }
        else
        {
            e.Row.Cells[1].Text = Math.Abs(id).ToString();
            e.Row.Cells[1].ForeColor = System.Drawing.Color.Red;
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM