Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to back color alternate rows in a gridview. It works. At the same time how to color back of controls of alternate rows with the same color. For boundfield it is OK but for other controls like Textbox , how to make it. The back color of other control is white and does not look nice.

XML
<asp:GridView ID="grvcart" runat="server"   style=" margin-top:447px; margin-left:450px ; "   BorderStyle="Groove" BorderWidth="1" BorderColor="White"
                ShowFooter="True" AutoGenerateColumns="False" GridLines="None"  Width="787" HeaderStyle-Font-Size="Small" HeaderStyle-ForeColor="White"
                CellPadding="4" ForeColor="#999999"  OnRowDeleting="grvcart_RowDeleting" HeaderStyle-BackColor="#666666"  RowStyle-BackColor="White" AlternatingRowStyle-BackColor="#cccccc" >
    <Columns>
        <asp:BoundField DataField="RowNumber" HeaderText="SNo"  />

        <asp:TemplateField HeaderText="Product Id" >
        <ItemTemplate>
        <asp:TextBox ID="TBProductId" Width="44"     runat="server" ReadOnly="true" ForeColor="Black"  BorderStyle="None" Style=" text-align:center"
        Text=''></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
Posted

Try below code:

protected void grvcart_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowIndex % 2 == 0)
{
TextBox TBProductId = (TextBox)e.Row.FindControl("TBProductId");
TBProductId.BackColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
}
else
{
TextBox TBProductId = (TextBox)e.Row.FindControl("TBProductId");
TBProductId.BackColor = System.Drawing.ColorTranslator.FromHtml("#cccccc");
}
}

}
 
Share this answer
 
Comments
S.Rajendran from Coimbatore 13-Mar-14 9:51am    
Fine. Thanks. It works.
Hi..
See this its help full to u using jquery.
XML
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript">
           $(document).ready(function () {
$("#TBProductId").css({"background-color": "colorname"});
});
</script>

Thank u.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900