WorknHardr
asked on
GridView Render Alternate Row Color Two White Two Gray?
I need a GridView to display data with alternating row-pair row colors as:
1 & 2 = white
3 & 4 = gray
5 & 6 = white
etc...
I'm using the Render(HtmlTextWriter) method to produce alt row colors.
Note:
1 - the rows/columns are rendered dynamically with a dataset
2- the Gridview has AutoGenerateColumns="true"
[aspx]
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" >
<Columns>
</Columns>
</asp:GridView>
[.cs]
protected override void Render(HtmlTextWriter writer)
{
if (GridView1.Rows.Count != 0)
{
Table gridTable = (Table)GridView1.Controls[ 0];
string lastValue = string.Empty;
string currentValue = string.Empty;
foreach (GridViewRow row in GridView1.Rows)
{
int rowIndex = gridTable.Rows.GetRowIndex (row);
if (rowIndex % 2 != 0) //alt row colors, need 2-white 2-gray
{
row.BackColor = System.Drawing.Color.Gains boro;
}
}
}
base.Render(writer);
}
1 & 2 = white
3 & 4 = gray
5 & 6 = white
etc...
I'm using the Render(HtmlTextWriter) method to produce alt row colors.
Note:
1 - the rows/columns are rendered dynamically with a dataset
2- the Gridview has AutoGenerateColumns="true"
[aspx]
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"
<Columns>
</Columns>
</asp:GridView>
[.cs]
protected override void Render(HtmlTextWriter writer)
{
if (GridView1.Rows.Count != 0)
{
Table gridTable = (Table)GridView1.Controls[
string lastValue = string.Empty;
string currentValue = string.Empty;
foreach (GridViewRow row in GridView1.Rows)
{
int rowIndex = gridTable.Rows.GetRowIndex
if (rowIndex % 2 != 0) //alt row colors, need 2-white 2-gray
{
row.BackColor = System.Drawing.Color.Gains
}
}
}
base.Render(writer);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent! thx :)
Open in new window