Radgrid cell formatting problem

2 posts, 1 answers
  1. Karthik
    Karthik avatar
    7 posts
    Member since:
    Oct 2012

    Posted 26 Oct 2012 Link to this post

    Hi Telerik,
    I have assigned some cols as read only in the radgrid(radform) and i used cell formatting to apply colors like this by name:
    DTURradGridView.Columns["dtuct0"].ReadOnly = true;
              DTURradGridView.Columns["dtuct1"].ReadOnly = true;
              DTURradGridView.Columns["dtuct2"].ReadOnly = true;
              DTURradGridView.Columns["dtuct3"].ReadOnly = true;
              DTURradGridView.Columns["dtuy"].ReadOnly = true;

    if (e.CellElement.ColumnInfo is GridViewDataColumn)
                {
                    if (e.CellElement.ColumnInfo.FieldName == "dtuct0" )
                    {
                        e.CellElement.DrawFill = true;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = Color.LightSlateGray;
                        e.CellElement.GradientStyle = GradientStyles.Linear;
                    }
                    if (e.CellElement.ColumnInfo.FieldName == "dtuct1")
                    {
                        e.CellElement.DrawFill = true;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = Color.LightSlateGray;
                        e.CellElement.GradientStyle = GradientStyles.Linear;
                    }
                    if (e.CellElement.ColumnInfo.FieldName == "dtuct2")
                    {
                        e.CellElement.DrawFill = true;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = Color.LightSlateGray;
                        e.CellElement.GradientStyle = GradientStyles.Linear;
                    }
                    if (e.CellElement.ColumnInfo.FieldName == "dtuct3")
                    {
                        e.CellElement.DrawFill = true;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = Color.LightSlateGray;
                        e.CellElement.GradientStyle = GradientStyles.Linear;
                    }
                    if (e.CellElement.ColumnInfo.FieldName == "dtuy")
                    {
                        e.CellElement.DrawFill = true;
                        e.CellElement.NumberOfColors = 1;
                        e.CellElement.BackColor = Color.LightSlateGray;
                        e.CellElement.GradientStyle = GradientStyles.Linear;
                    }
                }

    Then i want to hide/unhide based on the selection like this:
    private void normalToolStripMenuItem_Click(object sender, EventArgs e)//Normal View
            {
                DTURradGridView.Columns["dtuct0"].IsVisible = false;
                DTURradGridView.Columns["dtuct1"].IsVisible = false;
                DTURradGridView.Columns["dtuct2"].IsVisible = false;
                DTURradGridView.Columns["dtuct3"].IsVisible = false;
                DTURradGridView.Columns["dtuy"].IsVisible = false;
     
            }
     
             
     
            private void advancedToolStripMenuItem_Click(object sender, EventArgs e) //Advanced View
            {
                DTURradGridView.Columns["dtuct0"].IsVisible = true;
                DTURradGridView.Columns["dtuct1"].IsVisible = true;
                DTURradGridView.Columns["dtuct2"].IsVisible = true;
                DTURradGridView.Columns["dtuct3"].IsVisible = true;
                DTURradGridView.Columns["dtuy"].IsVisible = true;
     
          
            }
    When i Click on Normal Mode function, it applies colors to other cols. This is really strange for me. Please help me, thank you.
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 07 Nov 2012 Link to this post

    Hello,

    Because the grid view's cells are virtualized, the grid reuses cell, so you need to reset the colors of the cell if none of the conditions match your case.

    // previous code from cell formatting
    if (...)
    {
         ...
    }
    else
    {
          e.CellElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
          // and also reset any other value you have set manually
    }

    If you have any other questions, please let me know.


    Best Regards,
    Emanuel Varga
    Winforms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
Back to Top