GridCellElement reference not set to an instance of an object

6 posts, 1 answers
  1. Georges
    Georges avatar
    4 posts
    Member since:
    Jul 2010

    Posted 26 Jul 2010 Link to this post

    Hi,

    I work with radGridView and color some cells according to the option set by user. So I created a function to color/uncolor cell of the grid. Bellow the fonction to reset the color of the (second columns of the) grid :

    private bool ResetCellsColor(RadGridView radGridView)
    {
        Boolean ret = true;
     
        try
        {
            GridViewColumn gvc = null;
             
            if (radGridView.Columns.Count > 2)
            {
                gvc = radGridView.Columns[2] as GridViewColumn;
            }
     
            if (gvc == null)
            {
                ret = false;
            }
            else
            {
                for (int i = 0; i < radGridView.Rows.Count; i++)
                {
                    if (radGridView.Rows[i].IsVisible)
                    {
                        GridViewRowInfo gvri = radGridView.GridElement.GridViewInfo.Rows[i] as GridViewRowInfo;
                        if (gvri == null)
                        {
                            ret = false;
                            continue;
                        }
     
                        GridCellElement gce = radGridView.TableElement.GetCellElement(gvri, gvc) as GridCellElement;
                                                     
                        if (gce != null)
                        {
                            gce.ResetValue(VisualElement.ForeColorProperty);
                            gce.ResetValue(VisualElement.FontProperty);
                            gce.DrawFill = false;
                        }
                        else
                        {
                            ret = false;
                        }
                    }
                }
            }
        }
        catch{ ret = false; }
         
        return ret;
    }


    This function work well in most of cases, but when I delete (all) the rows of my grid and add new ones and then call this function again, that doesn't work : the GridCellElement returns null. If I give the control to the user and then call again the function, it will work.
    In my case, it works for the 4 first line of my radGridView (that contains 15 rows). All the rows are threated the same way and the Value of all cells are defined and accessible.

    It seems that the source of the problem is in the radGridView.TableElement.VisualRows where some items are missing. They are only four element (instead of 15) in VisualRows object.

    Do you have an idea to how solve the problem ?


    Best regards,

    Georges DE VOS.




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

    Posted 26 Jul 2010 Link to this post

    Hello Georges,

    If you just want to color the rows or cells of a grid you should just use the CellFormatting event:
    Telerik Documentation Formatting Cells
    or RowFormatting event:Telerik Documentation Formatting Rows.

    If you require any more help or if i misunderstood something please let me know.
    Emanuel, Varga
  3. Georges
    Georges avatar
    4 posts
    Member since:
    Jul 2010

    Posted 27 Jul 2010 Link to this post

    Hello Emanuel Varga,

    It works well.

    Thank you

    Georges DE VOS.



  4. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 29 Jul 2010 Link to this post

    Hello Georges,

    Thank you for your question.

    Emanuel's answer is fine. As long as the RadGridView control uses UI virtualization, GridCellElement and GridRowElement should be used only in formatting events. Please refer to this blog post for more information about virtualization and formatting.

    Kind regards,
    Alexander
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Bao
    Bao avatar
    54 posts
    Member since:
    Jan 2015

    Posted 26 Jul 2017 Link to this post

    Hi,

    I want to loop the gridview rows to set value for custom cell (checkboxcell element) by using TableElement.GetCellElement at the time i click button. How can i use it in click button event.

  6. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3573 posts

    Posted 27 Jul 2017 Link to this post

    Hello Bao, 

    Thank you for writing.  

    If you use custom cells, the SetContentCore method is the appropriate place to synchronize the relevant data. Additional information for custom cells is available here: http://docs.telerik.com/devtools/winforms/gridview/cells/creating-custom-cells

    Here is a sample code snippet demonstrating how to iterate the visual cell elements:
    private void radButton1_Click(object sender, EventArgs e)
    {
        foreach (GridRowElement rowElement in this.radGridView1.TableElement.VisualRows)
        {
            foreach (GridViewColumn col in this.radGridView1.Columns)
            {
                GridCellElement cell = this.radGridView1.TableElement.GetCellElement(rowElement.RowInfo, col);
                if (cell != null)
                {
                    Console.WriteLine(cell.Text);
                }
            }
        }
    }

    I hope this information helps. Should you have further questions I would be glad to help.

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top