I have a situation where I need to know the current color of an alternating row in a TemplateField of a GridView.
UPDATED:
How do I retrieve this Color value in a <%# ??? %>
.
(Or an workaround where I get the row number).
I have a situation where I need to know the current color of an alternating row in a TemplateField of a GridView.
UPDATED:
How do I retrieve this Color value in a <%# ??? %>
.
(Or an workaround where I get the row number).
This may be a weird work-around, but you could always check to see if the row you are checking for is an odd or even numbered row. If it is an even number, it is likely using the color set up in the AlternatingRow template. If it is even, it should be using the regular Row template.
How does the GridView change colour - every other row, or in chunks of X? Or is it set in some more "random" fashion?
If it's every other row, you can simply check if the row is a "normal" row or an "alternating" row.
I can't remember how it's written exactly, and I only found this VB example, but it might be of some help:
If e.Row.RowState = DataControlRowState.Normal Then
//do stuff
ElseIf e.Row.RowState = DataControlRowState.Alternate Then
//do other stuff
Where e is the GridView object. This doesn't check the actual colour of the row though. I suppose you should be able to do something like:
if(System.Drawing.Color.Red == e.Row.BackColor)
If you can, please be more detailed as to how the colour is set on each row.
To get the colors from inside of a <% %> tag in the template field itself, you could use this code...
<asp:TemplateField>
<ItemTemplate>
<%# ((GridViewRow)Container).RowState == DataControlRowState.Alternate ? ((GridView)((GridViewRow)Container).Parent.Parent).AlternatingRowStyle.BackColor : ((GridView)((GridViewRow)Container).Parent.Parent).RowStyle.BackColor%>
</ItemTemplate>
</asp:TemplateField>
You can also do this in the RowDataBound event of the GridView. In the RowDataBound command, you can query the e.Row.RowState to find out what type of row you are on. Values include DataControlRowState.Alternate and DataControlRowState.Normal. You can use the sender to grab the color based on that row type...
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
// set first cell in the row to color just for demonstration purpose.
if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Alternate)
{
e.Row.Cells[0].Text = ((GridView)sender).AlternatingRowStyle.BackColor.ToString();
}
}
Create this function in your codebehind page (or in a section in your .aspx page):
protected string GetColor(object container)
{
int ordinal = 0;
try
{
ordinal = int.Parse(DataBinder.Eval(container, "DataItemIndex").ToString());
}
catch (Exception)
{
ordinal = int.Parse(DataBinder.Eval(container, "ItemIndex").ToString());
}
return (ordinal % 2) == 0 ? "Row" : "Alternate Row";
}
Then in your markup, you'd call it like so:
<%# GetOrdinal(Container) %>
(Note the uppercase "Container").
check this site also for changing grid view color http://gridviewrowcolor.blogspot.com/