Problems with GridView.CssClass and Selections

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

Greetings,

On a GridView control, I want to show alternating colors. But instead of
alternating on each row, I want to alternate each group. So several items
would be one color and the next several items would be another color.

I'm able to accomplish this by setting the CssClass property in the
GridView's RowDataBound event handler.

But I also need the rows to be selectable so I set the CssClass for the
SelectedRowStyle at design time. The problem is that, when CssClass is set
in the RowDataBound event handler, that style is always used, even when the
row is selected. And these rows no longer provide any visual indication that
they are selected.

It appears that ASP.NET will not override a manually set CssClass with the
selected row CssClass.

Does anyone know how I could make this work?

Thanks.
 
Greetings,

On a GridView control, I want to show alternating colors. But instead of
alternating on each row, I want to alternate each group. So several items
would be one color and the next several items would be another color.

I'm able to accomplish this by setting the CssClass property in the
GridView's RowDataBound event handler.

But I also need the rows to be selectable so I set the CssClass for the
SelectedRowStyle at design time. The problem is that, when CssClass is set
in the RowDataBound event handler, that style is always used, even when the
row is selected. And these rows no longer provide any visual indication that
they are selected.

It appears that ASP.NET will not override a manually set CssClass with the
selected row CssClass.

Does anyone know how I could make this work?

Thanks.

As you are setting the normal styles at run time it is necessary to do
the same the for the selected row. In the RowDatabound event handler
where you are assigning the CssClass you can check whether the row
being processed is selected or not by comparing e.row.RowIndex with
the GridView SelectedIndex property. If they match then apply the
selected row style - you could even vary this according to which group
the row is in.
 
Back
Top