我想在不更改标题颜色的情况下更改我的gridview行的颜色
这是我的数据绑定功能
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
DateTime Kbl = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "HrsKbl"));
foreach (TableCell cell in e.Row.Cells)
{
if (Kbl == DateTime.Now)
{
cell.BackColor = Color.Yellow;
}
if (Kbl > DateTime.Now)
{
cell.BackColor = Color.Green;
}
if (Kbl < DateTime.Now)
{
cell.Backcolor = Color.Red;
}
}
}
这是我的gridview
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="Katalog" CssClass="myGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt" AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="None" OnPageIndexChanging="exportGrdVw_PageIndexChanging" OnRowDataBound="OnRowDataBound">
<AlternatingRowStyle BackColor="White" CssClass="alt" />
<Columns>
<asp:TemplateField HeaderText="No" HeaderStyle-Font-Italic="true">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle Font-Italic="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="ID Pinjam" HeaderStyle-Font-Italic="true">
<ItemTemplate>
<asp:Label ID="LabelID" runat="server" Text='<%#Eval("IDPinjam") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Font-Italic="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Katalog" HeaderStyle-Font-Italic="true">
<ItemTemplate>
<asp:Label ID="LabelKatalog" runat="server" Text='<%#Eval("Katalog") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Font-Italic="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Kbl" HeaderStyle-Font-Italic="true" Visible="false">
<ItemTemplate>
<asp:Label ID="LabelKbl" runat="server" Text='<%#Eval("HrsKbl") %>' Visible="false"></asp:Label>
</ItemTemplate>
<HeaderStyle Font-Italic="True" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-HorizontalAlign="Center" ItemStyle-Font-Italic="true">
<ItemTemplate>
<asp:LinkButton ID="LinkEdit" runat="server" CssClass="myButton" Text="Kembalikan" OnClick="LinkEdit_Click"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Font-Italic="True" />
</asp:TemplateField>
</Columns>
<RowStyle CssClass="RowStyle" />
<EmptyDataRowStyle CssClass="EmptyRowStyle" />
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
当我运行它时,我的gridview的标题总是将其颜色更改为“红色”,就像我的行数据绑定的第三个条件
答案 0 :(得分:1)
试试这个,if会检查它是否实际上是一行而没有页眉,页脚等
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime Kbl = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "HrsKbl"));
foreach (TableCell cell in e.Row.Cells)
{
if (Kbl == DateTime.Now)
{
cell.BackColor = Color.Yellow;
}
if (Kbl > DateTime.Now)
{
cell.BackColor = Color.Green;
}
if (Kbl < DateTime.Now)
{
cell.Backcolor = Color.Red;
}
}
}
}
答案 1 :(得分:0)
除了恩里克的回答之外,还有一些事情需要考虑:
DateTime.Now
都会创建并返回一个填充当前时间的DateTime结构。这种情况发生得如此之快,以至于网格中只有几行您可能看不到差异。但是列表越长,您就越有可能看到“漂移”,第1行中的DateTime.Now
与第N行中DateTime.Now
的时间不匹配尝试这样做:
DateTime timestamp;
protected void OnDataBinding(object sender, EventArgs e)
{
timestamp = DateTime.Now ;
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime Kbl = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "HrsKbl"));
e.Row.BackColor = (Kbl == timestamp ? Color.Yellow :
(Kbl > timestamp ? Color.Green : Color:Red));
}
}