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

Setting Background Cell colour using ColorSelector

1 Answer 90 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Derek
Top achievements
Rank 1
Derek asked on 18 Dec 2008, 11:47 AM
Hi,
I have a command button that opens the colorselector dialog. I would like to return that color and set the background of a cell, in other words cell 2 for row 3 for example.  Is there a quick and easy way to do this? nothing has worked for me so far.

Thanks,
Derek.

1 Answer, 1 is accepted

Sort by
0
Nick
Telerik team
answered on 18 Dec 2008, 04:53 PM
Hi Derek,

Thank you for your question.

Please see the following code snippet:

private void radGridView1_CommandCellClick(object sender, EventArgs e) 
        { 
            ((GridCommandCellElement)sender).RowInfo.Tag = Color.Red;      
            ((GridCommandCellElement)sender).RowInfo.InvalidateRow(); 
        } 
 
        private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e) 
        { 
            if (e.CellElement.RowInfo.Tag is Color && e.CellElement.ColumnInfo.HeaderText=="column1"
            { 
                e.CellElement.DrawFill = true
                e.CellElement.BackColor = (Color)e.CellElement.RowInfo.Tag; 
            } 
        } 

You must use CellFormatting to apply formatting and CommandCellClicked to handle the buttons' click events. You have to save the desired color in the RowInfo tag and then use it in CellForamatting event. Note that you have to invalidate the Row so that the change becomes visible. Note also that the Tag property differs substantially in RowInfo and CellElement. The former is a property of every row so when you scroll it works ok. The latter is a property only of the visible cells. The grid uses UI virtualization - CellElements are reused when you scroll - so saving the Color in its tag does not make sense.

Do not hesitate to contact me back if you have more questions.

All the best,
Nick
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
GridView
Asked by
Derek
Top achievements
Rank 1
Answers by
Nick
Telerik team
Share this question
or