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

GridCellElement reference not set to an instance of an object

5 Answers 237 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Georges
Top achievements
Rank 1
Georges asked on 26 Jul 2010, 11:38 AM
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.




5 Answers, 1 is accepted

Sort by
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 26 Jul 2010, 03:12 PM
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
0
Georges
Top achievements
Rank 1
answered on 27 Jul 2010, 09:29 AM
Hello Emanuel Varga,

It works well.

Thank you

Georges DE VOS.



0
Alexander
Telerik team
answered on 29 Jul 2010, 11:36 AM
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
0
Bao
Top achievements
Rank 1
answered on 26 Jul 2017, 03:58 PM

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.

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 27 Jul 2017, 08:58 AM
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.
Tags
GridView
Asked by
Georges
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Georges
Top achievements
Rank 1
Alexander
Telerik team
Bao
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or