Question

I have a GridView which is bound automaticaly to a SqlDataSource. I have a function which goes thru the GridView and colors red certain cells according to their content. I also enabled sorting on the GridView on all columns. When the page first loads the function coloring appropriate cells works. When I click the column header to sort, it sorts, but the cells that should be colored doen't get colored. I tried putting the call to the coloring function in the Page_Load in an if(isPostBack) condition and I tried using the GridView_Sorted event putting the call to the coloring function in there. None of that worked. I also tried adding GridView1.DataBind() in both the Page_Load and GridView_Sorted events before calling the coloring function and after. In all scenarios sorting worked but coloring did not! How can I get both sorting and coloring working? Thank you.

Was it helpful?

Solution

To enable accepting the answer, i copy my comment here:

Put your coloring logic into GridView's RowDataBound event.

OTHER TIPS

Put the coloring in the OnRowDataBound event.

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.ForeColor = Color.Red;
    e.Row.Cells[0].ForeColor = Color.Red;
}

Coloring the cells this way, it will happen during the databinding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top