2015-06-09 110 views
0

我想改变我的GridView行的颜色conditionaly没有改变标题颜色变化颜色没有改变标题颜色

这里是我的数据绑定功能

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标题始终其颜色更改为“红”得就像我行的数据绑定

回答

1

第三个条件尝试这一点,如果将检查它实际上是一排没有页眉,页脚等

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; 
     } 
    } 
    } 
} 
0

一几件事情,除了考虑恩里克的答案:

  1. 每次调用DateTime.Now创建并返回用当前时间的DateTime结构。这发生得如此之快,以至于在网格中只有几条线,你可能看不出有什么区别。但是,列表越长,您将看到“漂移”的可能性越大,因此第1行中的DateTime.Now与第N行中的DateTime.Now时间不匹配。
  2. 如果您尝试更改每行的颜色,则会更容易一次只能做一个细胞的方法。

尝试这样做,而不是:

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)); 
    } 
}