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

Chanaging the color of a cell using CellElement

9 Answers 470 Views
GridView
This is a migrated thread and some comments may be shown as answers.
mohammed abdel-aziz
Top achievements
Rank 1
mohammed abdel-aziz asked on 28 Jan 2010, 01:10 PM
i have download the trial version of your product (WinForms) to test it and give feed back to my boss to decide if this will be useful to buy,
every this is alright except one issue,
pleas have a look on my code

dataGridView1.DataSource = tab; //where tab is returned from the database and have more than 100 row 
 for (int i = 0; i < dataGridView1.Rows.Count; i++)
     {
            try
                {
                    dataGridView1.Rows[i].Cells[0].CellElement.DrawFill = true;
                    dataGridView1.Rows[i].Cells[0].CellElement.BackColor = Color.Red;
                }
                catch
                { }
       }    
the loop stop every time on row number 9
it throw exception "Object reference not set to an instance of an object" on CellElement (CellElement is null)
is that because its a trial version?
i don't want to use Cell Formatting Event cuz this will not be helpful for me 
thanks

9 Answers, 1 is accepted

Sort by
0
HamiD Mayeli
Top achievements
Rank 1
answered on 28 Jan 2010, 01:56 PM

Hi Mohammed

You used Worng Code
Check is Code

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)  
     {  
            try 
                {  
                    dataGridView1.Rows[i].Cells[0].CellElement.DrawFill = true;  
                    dataGridView1.Rows[i].Cells[0].CellElement.BackColor = Color.Red;  
                }  
                catch 
                { }  
       }      
or this one

foreach (Telerik.WinControls.UI.GridViewDataRowInfo Row in dataGridView1.Rows)  
     {  
            try 
                {  
                    Row.Cells[0].CellElement.DrawFill = true;  
                    Row.Cells[0].CellElement.BackColor = Color.Red;  
                }  
                catch 
                { }  
       }   
0
mohammed abdel-aziz
Top achievements
Rank 1
answered on 28 Jan 2010, 02:08 PM

Hi HamiD
thanks for your reply
i have tried your code but still the same problem
what we want to do is
1-change the vell of a certain cell (done without problems)
2-color this cell with specified color for 1 second and then return the cell color to it's original color ,and the reset color method will be as for or foreach loop on all cells in datagrid to return them all to the original color ,and there is timer to run the rest method method

so the problem appear in step no 2 as sometimes the cell element is null when we try to change the cell color or retrieve it back to its original color

0
Jack
Telerik team
answered on 29 Jan 2010, 08:48 AM
Hello mohammed abdel-aziz,

RadGridView uses virtualization for its elements. It creates visual elements only for the rows and cells that are currently visible. When scrolling or sorting they are reused. You can't access a cell element directly. You should handle CellFormatting event instead. This article in our online documentation describes how to access and customize grid cells. More information is available in this KB article. Should you have any further questions, don't hesitate to ask.

 

Sincerely yours,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
mohammed abdel-aziz
Top achievements
Rank 1
answered on 29 Jan 2010, 11:19 AM
hi Jack ,
thank you for your reply.
i know that CellFormatting event is the best way to customize the appearance of any cell but in my situation i want to change the color of specific cell on my Client application based on updates coming from the server and change back the cell color to its initial state after tow seconds, i have done that so easily using .net DataGridView and .net Timer cuz i can access the cell color directly any time in .net DataGridView, How can i accomplish that senario in RadGridView.

thanks in advance.       
0
Jack
Telerik team
answered on 01 Feb 2010, 10:07 AM
Hello mohammed abdel-aziz,

I understand. Still you have to use CellFormatting event. However, you can cause this event to fire by calling Update method of GridElement. Here is a sample:

 

this.radGridView1.GridElement.Update(GridUINotifyAction.StateChanged);

 

Regards,
Jack
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
mohammed abdel-aziz
Top achievements
Rank 1
answered on 01 Feb 2010, 02:17 PM
hello Jack,
thank you for your reply.
i have used the update method in RadGridView and it solves half of the problem, the other half is how to pass data to that method to change the cell color depend on that data, something like that 
public void update_prices(PricesUpdates[] updates) 
        { 
            if (updates.Length > 0) 
            { 
                for (int i = 0; i < updates.Length; i++) 
                { 
                    double number; 
                    string oldValue = ultraGrid1.Rows.Single(e => e.Cells["Symbol"].Value.ToString() == updates[i].Stock && e.Cells["Market"].Value.ToString() == updates[i].Market).Cells[updates[i].Attribute].Value.ToString(); 
                    int index = -1; 
                    index = ultraGrid1.Rows.IndexOf(ultraGrid1.Rows.Single(e => e.Cells["Symbol"].Value.ToString() == updates[i].Stock && e.Cells["Market"].Value.ToString() == updates[i].Market)); 
                    ultraGrid1.Rows[index].Cells[updates[i].Attribute].Value = updates[i].Value; 
                    bool result = double.TryParse(oldValue, out number); 
                
                 
                    if (result == true
                    { 
 
                        if (double.Parse(ultraGrid1.Rows.Single(e => e.Cells["Symbol"].Value.ToString() == updates[i].Stock && e.Cells["Market"].Value.ToString() == updates[i].Market).Cells[updates[i].Attribute].Value.ToString()) < double.Parse(oldValue)) 
                        { 
                            ultraGrid1.Rows[index].Cells[updates[i].Attribute].BackColor = Color.Red; 
                        } 
                        else 
                        { 
                            ultraGrid1.Rows[index].Cells[updates[i].Attribute].BackColor = Color.Green; 
                        } 
                    } 
                    else 
                    { 
                        ultraGrid1.Rows[index].Cells[updates[i].Attribute].BackColor = Color.Red; 
                    } 
 
                } 
             
            } 
        } 

note that this data is not fixed it comes from the server periodically.
thank you
0
Accepted
Jack
Telerik team
answered on 04 Feb 2010, 08:38 AM
Hi mohammed abdel-aziz,

Your code should look something like this:

void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridViewDataColumn dataColumn = e.CellElement.ColumnInfo as GridViewDataColumn;
    if (dataColumn != null && dataColumn.UniqueName == "Symbol")
    {
        if (SomeCondition(e.CellElement.RowInfo, dataColumn))
        {
            e.CellElement.BackColor = Color.Red;
        }
        else
        {
            e.CellElement.BackColor = Color.Green;
        }
        e.CellElement.DrawFill = true;
        e.CellElement.GradientStyle = GradientStyles.Solid;
    }
}

Calling the Update method causes all cells in RadGridView to be updated. You have only to handle CellFormatting event and process the cells that meet certain condition. I hope this helps.

Regards,
Jack
the Telerik team


Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Follow the status of features or bugs in PITS and vote for them to affect their priority.
0
Makarand
Top achievements
Rank 1
answered on 20 Oct 2011, 06:57 AM
in wpf radgridview there is no CellFormatting,then how can i change cell style.
0
Stefan
Telerik team
answered on 20 Oct 2011, 10:12 AM
Hello Santosh,

Thank you for writing.

Please note that this forms concerns RadControls for WInForms and not RadControls for WPF. If you need assistance with our WPF products, please post your question in the appropriate forums

Here is a link from the online documentation of RadControls for WPF, regarding the desired functionality: http://www.telerik.com/help/wpf/gridview-cell-style-selector.html.

If you have any additional questions, feel free to post them in the WPF forums.
 
Regards,
Stefan
the Telerik team

Q2’11 SP1 of RadControls for WinForms is available for download (see what's new); also available is the Q3'11 Roadmap for Telerik Windows Forms controls.

Tags
GridView
Asked by
mohammed abdel-aziz
Top achievements
Rank 1
Answers by
HamiD Mayeli
Top achievements
Rank 1
mohammed abdel-aziz
Top achievements
Rank 1
Jack
Telerik team
Makarand
Top achievements
Rank 1
Stefan
Telerik team
Share this question
or