使用asp.net C#gridview基于条件更改行背景颜色

时间:2012-08-15 16:41:35

标签: c# asp.net datagridview

我想弄清楚当Time列的值大于30时如何更改行背景颜色。以下是我的代码:

 
<asp:GridView ID="gvMyDetails" runat="server" AutoGenerateColumns="False" AllowSorting="true" CellPadding="0"
    Width="730px" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
    Font-Bold="True" ForeColor="Black" OnSelectedIndexChanged="gvMyDetails_SelectedIndexChanged"
    Style="text-align: center">
    <Columns>
        <!-- Other asp:TemplateFields... -->
        <asp:TemplateField HeaderText="Time" HeaderStyle-BackColor="#4b6c9e">
            <ItemTemplate>
                &nbsp; &nbsp;
                <asp:Label ID="lblTime" runat="server" Text='<%# Bind("Time") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
SqlDataReader myReader4 = null;
SqlCommand myCommand4 = new SqlCommand("SELECT Account_Number, Std_Note_Code, Std_Note_Description, CONVERT(varchar(12), Std_Note_Date_Entered, 101) as datedate, Std_Note_Time_Entered, Time FROM qa_bor, qa_users where (qa_bor.Std_Note_Creator_Name = qa_users.username_bor and qa_users.username_bor = '" + ddlrep.Text + "') and (qa_bor.Std_Note_Date_Entered >= '" + datefrom + "' and qa_bor.Std_Note_Date_Entered <= '" + dateto + "') ORDER BY Std_Note_Date_Entered ASC", myConnection);
myReader4 = myCommand4.ExecuteReader();
if (myReader4.HasRows)
{
    gvMyDetails.DataSource = myReader4;                                
    gvMyDetails.DataBind();
    myReader4.Close();               
}

1 个答案:

答案 0 :(得分:1)

以下示例:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    e.Row.Attributes.Add("style", "cursor:help;"); 
    if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate) 
    {  
        if (e.Row.RowType == DataControlRowType.DataRow) 
        {                 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'"); 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#E56E94'"); 
            e.Row.BackColor = Color.FromName("#E56E94");                 
        }            
    } 
    else 
    { 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='orange'"); 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='gray'"); 
            e.Row.BackColor = Color.FromName("gray");                 
        }