2012-08-15 41 views
2

我正在製作一張預約書...我需要執行以下操作...更改asp.net c#上gridview上的行或記錄的顏色?

顯示尚未出席的所有約會(我有一個按鈕將其標記爲已出席).. 如果約會沒有參加,它的日期是在今天的日期之前,該行必須顯示爲紅色(文本字體顏色)...

所以我的問題是,如何更改特定行的顏色,並且如果在查詢中必須獲得某些內容以指定該行必須具有不同的顏色?

謝謝

<asp:GridView ID="grdvEventosVendedor" runat="server" AllowPaging="True" 
       AutoGenerateColumns="False" CellPadding="4" DatakeyNames="idCita" 
       EmptyDataText="No Hay Eventos Para Este Vendedor" ForeColor="#333333" 
       GridLines="None" AllowSorting="True" 
       onpageindexchanging="grdvEventosVendedor_PageIndexChanging" 
       onrowcommand="grdvEventosVendedor_RowCommand" 
       onsorting="grdvEventosVendedor_Sorting" CellSpacing="1" 
       onrowdatabound="grdvEventosVendedor_RowDataBound" > 
       <AlternatingRowStyle BackColor="White" ForeColor="#284775"/> 

       <Columns> 
        <asp:TemplateField HeaderText="" ItemStyle-Width="35px"> 
         <ItemTemplate> 
          <asp:ImageButton ID="imgBtnEdicEvento" runat="server" 
           CommandArgument='<%# Eval("idCita")%>' CommandName="Edicion" 
           Height="32px" ImageUrl="~/img/pencil_32.png" Width="32px" /> 
         </ItemTemplate> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="" ItemStyle-Width="35px"> 
         <ItemTemplate> 
          <asp:ImageButton ID="imgBtnDelete" runat="server" 
           CommandName="Borrar" 
           ImageUrl="~/img/1385_Disable_16x16_72.png" 
           onclientclick="return confirm('¿Desea eliminar el registro?');" 
           CommandArgument='<%# Eval("idCita")%>' /> 
         </ItemTemplate> 
        </asp:TemplateField> 

        <asp:BoundField DataField="Cliente" HeaderText="Cliente" InsertVisible="False" ReadOnly="True" SortExpression="Cliente" ItemStyle-Width="50px" /> 
        <asp:BoundField DataField="Empresa" HeaderText="Empresa" InsertVisible="False" ReadOnly="True" SortExpression="Empresa" ItemStyle-Width="50px"/> 
        <asp:BoundField DataField="Telefono" HeaderText="Telefono" InsertVisible="False" ReadOnly="True" SortExpression="Telefono" ItemStyle-Width="50px"/> 
        <asp:BoundField DataField="Nextel" HeaderText="Nextel" InsertVisible="False" ReadOnly="True" SortExpression="Nextel" ItemStyle-Width="50px"/> 
        <asp:BoundField DataField="Tipo" HeaderText="Tipo" InsertVisible="False" ReadOnly="True" SortExpression="Tipo" ItemStyle-Width="50px"/> 
        <asp:BoundField DataField="Descripcion" HeaderText="Descripcion" InsertVisible="False" ReadOnly="True" SortExpression="Descripcion" ItemStyle-Width="100px"/> 

        <asp:TemplateField HeaderText="Fecha" SortExpression="Fecha" ItemStyle-Width="50px"> 
         <ItemTemplate>            
          <%# DataBinder.Eval(Container.DataItem, "Fecha", "{0:dd/MM/yyyy}")%> 
         </ItemTemplate> 

         <EditItemTemplate>           
          <asp:TextBox ID="tbxFecha" runat="server" Text='<%#Bind("Fecha","{0:dd/MM/yyyy}") %>' ValidationGroup="gpEdicionAgenda"> 
          </asp:TextBox> 
         </EditItemTemplate> 
        </asp:TemplateField> 

        <asp:BoundField DataField="HoraInicio" HeaderText="Hora" InsertVisible="False" ReadOnly="True" SortExpression="HoraInicio" ItemStyle-Width="50px"/> 
        <asp:BoundField DataField="Lugar" HeaderText="Lugar" InsertVisible="False" ReadOnly="True" SortExpression="Lugar" ItemStyle-Width="50px"/> 
        <asp:BoundField DataField="Estado" HeaderText="Estado" InsertVisible="False" ReadOnly="True" SortExpression="Estado" ItemStyle-Width="50px"/> 

        <asp:TemplateField HeaderText="Atender" ItemStyle-Width="35px"> 
         <ItemTemplate> 
          <asp:ImageButton ID="imgBtnAtender" runat="server" 
           CommandArgument='<%# Eval("idCita")%>' CommandName="Atender" 
           ImageUrl="~/img/activar.png" Width="16px" Height="16px" 
           onclientclick="return confirm('¿Desea marcar como atendido este registro?');" /> 
         </ItemTemplate> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="CRM" ItemStyle-Width="35px"> 
         <ItemTemplate> 
          <asp:ImageButton ID="imgBtnCRM" runat="server" 
           CommandArgument='<%# Eval("IdCliente")%>' CommandName="CRM" 
           ImageUrl="~/img/go.gif" Width="16px" Height="16px" /> 
         </ItemTemplate> 
        </asp:TemplateField> 

        <asp:TemplateField HeaderText="VM" ItemStyle-Width="35px"> 
         <ItemTemplate> 
          <asp:ImageButton ID="imgBtnVerMas" runat="server" 
           CommandArgument='<%# Eval("IdCliente")%>' CommandName="VerMas" 
           ImageUrl="~/img/search.png" Width="16px" Height="16px" /> 
         </ItemTemplate> 
        </asp:TemplateField> 
       </Columns> 

       <EditRowStyle BackColor="#999999" Font-Size="Small" /> 
       <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
       <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Font-Size="Larger" /> 
       <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
       <RowStyle BackColor="#F7F6F3" ForeColor="#333333" HorizontalAlign="Center" Font-Size="Small" /> 
       <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
       <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
       <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
       <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
       <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
      </asp:GridView> 

回答

5

您可以通過指定行的背景色屬性的顏色改變行的顏色,你可以在grdvEventosVendedor_RowDataBound事件的GridView

e.Row.BackColor = Drawing.Color.Red; 

的指定或者,你可以使用

`e.Row.BackColor = Color.FromName("#FFOOOO");` 

你可以這樣做來實現以上的事情:

protected void grdvEventosVendedor_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType = DataControlRowType.DataRow) 
    { 
     if(put_your_condition_here) 
     { 
      e.Row.BackColor = Drawing.Color.Red; 
      //// or you can assign color by doing this: e.Row.BackColor = Color.FromName("#FFOOOO"); 
     } 
    } 
} 
+0

感謝您的回答,現在我試圖實現您的答案,但是,Drawing.Color.Redl不工作... 我已經添加使用System.Drawing; 但我不知道什麼是缺少的...... – Victor 2012-08-15 17:40:16

+0

使用Color.FromName(「#FF0000」)爲紅色..... – 2012-08-15 17:49:34

3

有許多的方式來處理這個問題,包括通過各行循環後的數據綁定(或)RowDataBound事件中:

foreach (GridViewRow row in yourGridView.Rows) 
{ 
    // Find controls/data to do comparison against 
    e.Row.BackColor = Color.FromName("#FF0000"); 
} 

或者......

protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    // Watch out for header and footer rows here 
    e.Row.BackColor = Color.FromName("#FF0000");     
}