This is a migrated thread and some comments may be shown as answers.

GridView Cell Formating event.

2 Answers 69 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jonathan Zee
Top achievements
Rank 1
Jonathan Zee asked on 20 Sep 2010, 09:50 AM
Hi there,

I have just upgraded my application to the latest version and discovered a weird bug with the GridView_CellFormatting event.

The previous version was trying to format the colors of the current cell depending on the value of another cell in the same row.

To simplify this behaviour, we can just look at 3 columns in a grid. Column 1: ID, Column 2: Name, Column 3: Status

If the status is True, Column 1's colour would be Blue and if the status is False, then column 1's colour would be Red.

in the previous version, the code was working fine..but with the new version, it seems that the Name column would also be affected.

and everytime the grid is refreshed through some random action e.g. moving of scrollbars, the name column's forecolor would change randomly..to either Red or Blue when those colours should only be affecting column 1.

private void gvDocuments_CellFormatting(object sender, CellFormattingEventArgs e)
        {
            
                if (e.CellElement.RowIndex >= 0)
                {
                    GridViewColumn col = e.CellElement.ColumnInfo as GridViewColumn;
                    GridViewRowInfo row = e.CellElement.RowInfo;
                     
                    string fieldName = col.Name;
                    if (fieldName == "Id")
                    {
                        if (row.Cells["Status"].Value.ToString() == "T")
                        {
 
                            e.CellElement.ForeColor = Color.Blue;
                            e.CellElement.Font = font;
                        }
                        else
                        {
                            e.CellElement.ForeColor = Color.Red;
                            e.CellElement.Font = font;
                        }
                    }    
                }
          }

P.s. I think The #2 example in http://www.telerik.com/help/winforms/formatting_cells.html is wrong... RowInfo.Cells[ColumnName] does not contain the CellElement property.

e.CellElement.RowInfo.Cells["column1"].CellElement.ForeColor

2 Answers, 1 is accepted

Sort by
0
erwin
Top achievements
Rank 1
Veteran
Iron
answered on 20 Sep 2010, 12:20 PM
I have also noticed this effect.

I guess it is because of the new column virtualization feature which should increase performance for grids with lots of columns.
Had to change code to get values for dependent cells from the datasource instead of from the grid.

Regards
Erwin
0
Jack
Telerik team
answered on 23 Sep 2010, 07:52 AM
Erwin, thank you for your suggestion. Yes, you are correct. Most probably the issue appears because of our new UI virtualization model.

Jonathan, please consider the following blog article. It describes the UI Virtualization and the effect that it may cause. The GridView >> Customize >> Formatting with Code example from our demo application demonstrates exactly how to change a cell value based on another cell value. The main rule is to reset all properties when the condition fails. You have to modify your code the following way:
private void gvDocuments_CellFormatting(object sender, CellFormattingEventArgs e)
{
 
    if (e.CellElement.RowIndex >= 0)
    {
        GridViewColumn col = e.CellElement.ColumnInfo as GridViewColumn;
        GridViewRowInfo row = e.CellElement.RowInfo;
 
        string fieldName = col.Name;
        if (fieldName == "Id")
        {
            if (row.Cells["Status"].Value.ToString() == "T")
            {
 
                e.CellElement.ForeColor = Color.Blue;
                e.CellElement.Font = font;
            }
            else
            {
                e.CellElement.ForeColor = Color.Red;
                e.CellElement.Font = font;
            }
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.ForeColorProperty);
            e.CellElement.ResetValue(LightVisualElement.FontProperty);
        }
    }
}

I hope it helps. If you need further assistance, I will be glad to help.

Regards,
Jack
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Jonathan Zee
Top achievements
Rank 1
Answers by
erwin
Top achievements
Rank 1
Veteran
Iron
Jack
Telerik team
Share this question
or