[英]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.