.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Alternative colors for rows of gridview

Posted By: pooja     Posted Date: April 07, 2010    Points:2   Category :ASP.Net

I have a gridview. I want to have like first 3 rows must be in some color and next two rows must be in different color and so on...

how to do this? please help me

thanx in advance

Author: Dilip             
Posted Date: April 07, 2010     Points: 5   

Just a small confirmation..

Is it like for ex if we have 10 rows in the grid

1 - RED
2 - RED
3 - RED
4 - BLUE
5 - BLUE
6 - RED
7 - RED
8 - RED
9 - BLUE
10 - BLUE

Please confirm

Author: pooja             
Posted Date: April 07, 2010     Points: 5   

Hi dilip..

you are right...

first 2 rows must be some color and next 2 rows must be another color..

thank for response

Author: Amit Mehra             
Posted Date: April 07, 2010     Points: 5   


Why you are making your life complected? :) usually developer do alternate row different color in gridView and its good for the user also..and your manager asking 2 in the different color..

Ok..I am giving you how to do that using code behind. but you have create your own logic for 2 row in the same color

you can use RowDataBound event of the GridView to do that

for eg

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
if (e.Row.RowType != DataControlRowType.Footer || e.Row.RowType != DataControlRowType.Footer)
if (e.Row.RowIndex == 1 || e.Row.RowIndex == 2 ) //This will give you row index
e.Row.BackColor = System.Drawing.Color.Red;
e.Row.BackColor = System.Drawing.Color.Blue;

I know its not the full code but you can apply your logic for 2 in a row. hope this helps.

Please "Mark As Answer " if this post help you.
Author: Lalij Mer             
Posted Date: April 07, 2010     Points: 5   


Try this code sample

<asp:GridView ID="GridView1" AlternatingRowStyle-BackColor="Gray" runat="server" >

2.you can set pageSize=50 its show every 50 records in every pages

<asp:GridView ID="GridView1" onpageindexchanging="GridView1_PageIndexChanging" AlternatingRowStyle-BackColor="Gray" AllowPaging="true" PageSize="50" runat="server" >

add code in codebehin
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
GridView1.PageIndex = e.NewPageIndex;

Refer also this link

Please Check my answer if you help....
Thank You...


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend