Change backcolor of a cell when current row is changing

14 posts, 1 answers
  1. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    I have a scenario, where a user is editing a row. Based on whether this row is in a modified state or not, I want to paint the 5th cell ( i.e. 'Modified' column) with a skyblue back color when the current row is changing. I tried using a condition object in form load event, but it failed to apply when I leave the current row after changing it. When current row is changing, I am determining whether the row was modified and if yes , then I am setting the 'Modified' column to 'Yes', and I am having no problems with this part of the logic.
    I am using Q1 2008 SP1 version.

    My code for condition object that I used is:
    ConditionalFormattingObject obj =
       new ConditionalFormattingObject("MyCondition", ConditionTypes.Equal, "Yes", "",true);
                obj.CellBackColor = Color.SkyBlue;
                obj.CellForeColor = Color.Red;
                obj.TextAlignment = ContentAlignment.MiddleRight;
                attributesRadGridView.Columns["Modified"].ConditionalFormattingObjectList.Add(obj);
    How can I paint a cell in above scenario or paint the whole row? Either will do.

    Thanks
    Sunil
  2. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    Hello Sunil,

    For this i would suggest you use the CellFormatting event, and do the following:
    void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        GridDataCellElement cell = e.CellElement as GridDataCellElement;
        if (cell != null && cell.ColumnInfo.Name == "Modified")
        {
            if (cell.Value.Equals("YES"))
            {
                cell.GradientStyle = GradientStyles.Solid;
                cell.BackColor = Color.SkyBlue;
                cell.ForeColor = Color.Red;
                cell.TextAlignment = ContentAlignment.MiddleRight;
            }
            else
            {
                cell.ResetValue(LightVisualElement.GradientStyleProperty, ValueResetFlags.Local);
                cell.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
                cell.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
                cell.ResetValue(LightVisualElement.TextAlignmentProperty, ValueResetFlags.Local);
            }
        }
    }

    In the cell.Value please change the value if "YES" is not the value in the cell when it was modified.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Emanuel,

    I really appreciate all your help.

    Your suggestion to color the cell in cell formatting is part of the solution, but one also needs to color the cell as soon as the current row is changed. Cell formatting event will not fire when current row is changed without any scrolling, and this will not cause any coloring to happen. So one needs to take a 'two pronged approach' to coloring cells here.
    1. The cell formatting event is very useful since it colors the correct cells as one scrolls the grids. If the cell formatting event does not have coloring logic that includes restoring cell to original color, then as one scrolls a 'virtualized' grid, the cell color will not be be applied correctly to cells.
    2. One also needs to format the cell when current row is changed. I call a method called  'FormatChangesCell(e.OldRow.Cells["Changes"].CellElement);' that colors the old row cell appropriately and instantly, in the event 'CurrentRowChanged'. This method is as given below, and its main purpose is to color a cell called 'Changes' if its value is 'Yes', else restore original color for this cell.
    Thanks
    Sunil

    private void FormatChangesCell(Telerik.WinControls.UI.GridCellElement cellElement)
           {
     
               if (attributesRadGridView.Rows[cellElement.RowIndex].Cells[cellElement.ColumnIndex].Value.ToString() == "Yes")
               {
                   cellElement.GradientStyle = GradientStyles.Solid;
                   cellElement.NumberOfColors = 1;
                   cellElement.DrawFill = true;
                   cellElement.BackColor = Color.SkyBlue;
                   cellElement.ForeColor = Color.Red;
               }
               else
               {
                   cellElement.DrawFill = false;
                   cellElement.ResetValue(LightVisualElement.ForeColorProperty);
                   cellElement.ResetValue(VisualElement.BackColorProperty);
                   attributesRadGridView.Rows[cellElement.RowIndex].Cells[cellElement.ColumnIndex].Value = "No";
               }
     
           }
  5. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    Hello Sunil,

    Why do you need to call another format when CurrentRowChanging event is fired?
    The CellFormat event is fired for all the cells, always, so you don't have to do things twice (at least in the 2010 version that is)

    Again, sad to say, there is one behavior for the current release and a totally different one for the old one...

    Best Regards,
    Emanuel Varga
  6. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Hi Emanuel,

    In Q1 2008 SP1, the cell formatting event  does not fire when the current row is changed without scrolling. Scrolling in this version will cause it to fire. I tried using cell formatting event, and I noticed that as soon as I changed the row, the color did not apply. However, if I came back to that row then the blue color applied through cell formatting event. So I could not simply trust cell formatting event since it did not fire every time I changed a row.

    Thanks
    Sunil
  7. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Hi Emanuel,

    When exactly is the cell formatting event firing? The docs provide no detailed info on this.

    Thanks
    Sunil
  8. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    Hello Sunil,

    The CellFormat event fires every time the value of the cell was changed, so if you made some changes to the row, and the cell that says Modified will change, the CellFormat event will fire (at least in the 2010 version), so that's i said you can use that.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  9. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Hi Emanuel,

    When I tested, I found that cell formatting event only fires when the grid needs to update a row state or cell state.
    This happens when we scroll a 'virtualized' grid when rows need to be constantly updated as we scroll OR when a row has become a current row which means the row state needs to be updated.
    But when we just change a cell value, it should not fire a cell formatting event or a row formatting event.
    May be I am wrong?

    Thanks
    Sunil
  10. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    Sunil,

    I can give you the most basic example why the CellFormatting event is required to fire every time the cells value has been changed.
    The best example is a DateTime column, where you have a FormatString property,
    The Value of the cell is of a DateTime type, but the Cell.Text has to display the formatted value, so by a logical assumption the CellFormatting event has to fire every time a cell's value has been changed in order to provide formatting for the new value.

    Please let me know if there are any flaws in my reasoning

    P.s. I've also tested this behavior and i found it to be accurate (but i could have missed something)

    Best Regards,
    Emanuel Varga
  11. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Hi Emauel,

    What if you set a textboxcolumn cell value in code to "XYZ" while editing it and then click your cursor in another row. Will the cell formatting fire for the old cell where you set the value in code to "XYZ"?

    Thanks
    Sunil
  12. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    Yes, and it is firing for all the cells in the row apparently

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga
  13. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    Then it is a difference from Q1 2008 SP1 to the new version.
    I will keep that in mind when developing with the new grid.
    I appreciate your cooperation.

    Thanks
    Sunil
  14. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 20 Oct 2010 Link to this post

    There are a lot of differences between the grid q1 2010 and the q2 2010, what does that tell you about the differences between the 2008 version and the 2010? :)

    Sunil, one more thing, please remember to mark the questions that have been answered, if the question has been answered, this will help other people to find their answers faster.

    Like always, glad to be able to help,

    Best Regards,
    Emanuel Varga
  15. Sunil
    Sunil avatar
    230 posts
    Member since:
    Jan 2004

    Posted 20 Oct 2010 Link to this post

    I cannot mark the answer because the actual complete answer based on my version of grid is contained in one of my own replies, though you and Richard provided great help with your replies.

    I cannot mark my reply as the answer, it seems.

    Thanks
    Sunil
Back to Top
UI for WinForms is Visual Studio 2017 Ready