Reputation: 157
I'm attempting to format a GridView such that when the value in a column changes from the previous value, the font color changes. I've found numerous examples of how to do this and it seems pretty straightforward, but I can't get it to work. I'm using the following based on another example I found changing the background color.
protected void gvEscalationLinks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Debug.WriteLine("e.Row.DataItemIndex: " + e.Row.DataItemIndex);
if (e.Row.DataItemIndex == 0)
{
e.Row.Cells[1].BackColor = Color.AliceBlue;
return;
}
GridViewRow prevRow = gvEscalationLinks.Rows[e.Row.RowIndex - 1];
GridViewRow thisRow = e.Row;
e.Row.Cells[1].BackColor = (thisRow.Cells[1].Text == prevRow.Cells[1].Text) ? Color.AliceBlue : Color.White;
// no values are ever produced:
Debug.WriteLine("prevRow.Cells[1].Text: " + prevRow.Cells[1].Text);
Debug.WriteLine("thisRow.Cells[1].Text: " + thisRow.Cells[1].Text);
}
}
The problem is no values are returned for the current or previous row/cell, so this is always evaluates to true:
(thisRow.Cells[1].Text == prevRow.Cells[1].Text) ? Color.AliceBlue : Color.White;
Here's my GridView:
<asp:GridView ID="gvEscalationLinks" runat="server" OnRowDataBound="gvEscalationLinks_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Category">
<ItemTemplate>
<asp:Label ID="lblCategory" runat="server" Text='<%# Eval("escCategory") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SubCategory">
<ItemTemplate>
<asp:Label ID="lblSubCategory" runat="server" Text='<%# Eval("escSubCategory") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:HyperLink ID="lnkURL" NavigateUrl='<%# Eval("docurl") %>' runat="server"><%# Eval("linkname") %></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Supporting Manager">
<ItemTemplate>
<asp:Label ID="lblManager" runat="server" Text='<%# Eval("supportMgr") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
What am I doing wrong?
Upvotes: 1
Views: 719
Reputation: 157
I got this working as shown below. Getting the values from the current and previous rows like so:
GridViewRow thisRow = e.Row;
Label currentValue = thisRow.Cells[2].FindControl("lblSubCategory") as Label;
GridViewRow prevRow = gvEscalationLinks.Rows[e.Row.RowIndex - 1];
Label previousValue = prevRow.Cells[2].FindControl("lblSubCategory") as Label;
if (previousValue.Text != currentValue.Text)...
However, I had a lot of trouble actually getting the colors to alternate properly and ended up with something that just feels kludged to me:
protected void gvEscalationLinks_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow thisRow = e.Row;
Label currentValue = thisRow.Cells[2].FindControl("lblSubCategory") as Label;
if (e.Row.DataItemIndex == 0)
{
e.Row.Cells[1].BackColor = Color.AliceBlue;
Session["color"] = "AliceBlue";
return;
}
GridViewRow prevRow = gvEscalationLinks.Rows[e.Row.RowIndex - 1];
Label previousValue = prevRow.Cells[2].FindControl("lblSubCategory") as Label;
if (previousValue.Text != currentValue.Text)
{
if ( Session["color"].ToString() == "AliceBlue" )
{
e.Row.Cells[1].BackColor = Color.White;
Session["color"] = "White";
}
else
{
e.Row.Cells[1].BackColor = Color.AliceBlue;
Session["color"] = "AliceBlue";
}
}
else
{
if (Session["color"].ToString() == "AliceBlue")
{
e.Row.Cells[1].BackColor = Color.AliceBlue;
}
else
{
e.Row.Cells[1].BackColor = Color.White;
}
}
}
}
Upvotes: 1