2015-06-09 145 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)); 
    } 
}