C# 由于过期日期而更改gridview列颜色
我正在尝试更改gridview上的“过期日期”列。当我超过当前日期或在当前日期后15天内更改颜色时,我想将颜色更改为红色,如果从当前数据日期起15到30天,则将颜色设置为黄色。当我试图在设置datetime变量时实现rowdatabound时,我在gridview中缺少行,并且没有显示颜色C# 由于过期日期而更改gridview列颜色,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在尝试更改gridview上的“过期日期”列。当我超过当前日期或在当前日期后15天内更改颜色时,我想将颜色更改为红色,如果从当前数据日期起15到30天,则将颜色设置为黄色。当我试图在设置datetime变量时实现rowdatabound时,我在gridview中缺少行,并且没有显示颜色 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" OnDataBou
<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;
}
}
}
protectedvoid GridView1_RowDataBound1(对象发送方,GridViewRowEventArgs e)
{
对于(int i=0;i您不需要用于GridView1\u 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;
}
}
}
protectedvoid GridView1_RowDataBound1(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
字符串dateText=((Label)e.Row.FindControl(“Label1”)).Text;
如果(!string.IsNullOrEmpty(dateText))
{
DateTime dateValue=DateTime.ParseExact(dateText,“MM/dd/yyyy”,null);
如果(dateValue=DateTime.Now.AddDays(16)和&dateValueRowDataBound
为每行引发事件,您可以使用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;
}
}
}
}
protectedvoid GridView1_RowDataBound1(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
标签a=e.Row.FindControl(“Label1”)作为标签;
如果(a!=null)
{
日期时间lblDate;
if(!DateTime.TryParse(a.Text,out lblDate)
{
//日期时间转换不成功
//您的日期时间可能为空或无效
//在这种情况下做点什么
返回;
}
if(lblDate=DateTime.Now.AddDays(16)&&lblDate此操作将一直持续到日期的空值。我尝试添加了一个else,但它仍然无法通过空标签填充gridview。只会遵循逻辑直到空值。如果Label1.Text的值为空,您想做什么?我试图使该单元格的任何未输入/空值都变为白色。当我遇到一个ngridview停止日期单元格的ull值,而不是默认为蓝色并继续循环。如果我可以默认为蓝色或保留空白/白色并继续,则目标是为其余可能的值设置条件。我正在尝试for循环继续超过null值,但这不起作用。请参阅我编辑的答案,背面如果日期为空,则颜色将为蓝色