Chanaging the color of a cell using CellElement

10 posts, 1 answers
  1. mohammed abdel-aziz
    mohammed abdel-aziz avatar
    5 posts
    Member since:
    Jan 2010

    Posted 28 Jan 2010 Link to this post

    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
  2. HamiD Mayeli
    HamiD Mayeli avatar
    19 posts
    Member since:
    Oct 2009

    Posted 28 Jan 2010 Link to this post

    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 
                    { }  
           }   
  3. mohammed abdel-aziz
    mohammed abdel-aziz avatar
    5 posts
    Member since:
    Jan 2010

    Posted 28 Jan 2010 Link to this post

    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

  4. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 29 Jan 2010 Link to this post

    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.
  5. mohammed abdel-aziz
    mohammed abdel-aziz avatar
    5 posts
    Member since:
    Jan 2010

    Posted 29 Jan 2010 Link to this post

    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.       
  6. Jack
    Admin
    Jack avatar
    2333 posts

    Posted 01 Feb 2010 Link to this post

    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.
  7. mohammed abdel-aziz
    mohammed abdel-aziz avatar
    5 posts
    Member since:
    Jan 2010

    Posted 01 Feb 2010 Link to this post

    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
  8. Answer
    Jack
    Admin
    Jack avatar
    2333 posts

    Posted 04 Feb 2010 Link to this post

    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.
  9. Makarand
    Makarand avatar
    11 posts
    Member since:
    Oct 2011

    Posted 20 Oct 2011 Link to this post

    in wpf radgridview there is no CellFormatting,then how can i change cell style.
  10. Stefan
    Admin
    Stefan avatar
    2908 posts

    Posted 20 Oct 2011 Link to this post

    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.

Back to Top