GridView Cell Formating event.

3 posts, 0 answers
  1. Jonathan Zee
    Jonathan Zee avatar
    22 posts
    Member since:
    Mar 2009

    Posted 20 Sep 2010 Link to this post

    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. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 20 Sep 2010 Link to this post

    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
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jack
    Admin
    Jack avatar
    2335 posts

    Posted 23 Sep 2010 Link to this post

    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
Back to Top