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

NullReferenceException after editing a cell

8 Answers 223 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Phillip
Top achievements
Rank 1
Phillip asked on 17 Aug 2009, 03:45 PM
Hi,

I have an unbound simple grid with editable cells in it. I carry out a number of edits, which all work fine. But when the user wants to repopulate the grid, I call my refresh code, which includes completely rebuilding the grid. As part of that code I call

Telerik.WinControls.UI.

ColumnGroupsViewDefinition columnGroupsDefinition = this.radGridView.ViewDefinition as Telerik.WinControls.UI.ColumnGroupsViewDefinition;

 

columnGroupsDefinition.ColumnGroups.Clear();

 

if (this.radGridView.Columns.Count > 0) this.radGridView.Columns.Clear();

 

 

if(this.radGridView.Rows.Count > 0) this.radGridView.Rows.Clear();

 


To clear out the entire grid. I then follow a standard routing for repopulation adding columns and rows as required. Subsequently all attempts to set .CurrentRow, or to click between cells returns a NullReferenceException as shown in the stack below.

I suspect my problem is that I'm not "telling" the grid to clear itself properly but cannot find a wholesale "ClearAll" method. I also suspect the problem relates to tracking of the current Cell as there is a lot of dependence on there being a current cell inside EndEditCore, and its not explicitly checked for Null.

Can you assist ?

(My stack trace below).

An unhandled exception has occured with the following details
Message Object reference not set to an instance of an object.
Stack Trace
   at Telerik.WinControls.UI.RadGridView.EndEditCore(Boolean cancel, Boolean finishEditingOperation)
   at Telerik.WinControls.UI.RadGridView.EndEdit()
   at Telerik.WinControls.UI.RadGridView.ValidatePositionChange(GridViewRowInfo rowInfo, GridViewColumn column)
   at Telerik.WinControls.UI.BaseGridBehavior.OnMouseDownLeft(MouseEventArgs e)
   at Telerik.WinControls.UI.BaseGridBehavior.OnMouseDown(MouseEventArgs e)
   at Telerik.WinControls.UI.RadGridView.OnMouseDown(MouseEventArgs e)
   at System.Windows.Forms.Control.WmMouseDown(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at Telerik.WinControls.RadControl.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Additional Data
TicketNumber 21c900c6-0ab5-4f64-bc53-839a3a783e45

8 Answers, 1 is accepted

Sort by
0
Victor
Telerik team
answered on 18 Aug 2009, 10:58 AM
Hello Phillip,

I am afraid that I can not reproduce the described exception. Please send a sample application which demonstrates the behavior. Also please explain why do you need to populate/clear your grid manually. Can't this be achieved via data binding? Usually you don't need to manipulate the view definition of RadGridView directly, you simply have to call the Clear() methods of the Rows or Columns collections that are directly accessible through the RadGridView reference. I am looking forward to your reply.

Kind regards,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Phillip
Top achievements
Rank 1
answered on 18 Aug 2009, 03:14 PM
Victor,

A couple of points;
1) I find I do have to clear the ColumnGroupsDefinition otherwise columns within the group defintion are not cleared when I call Columns.Clear(). I tried this in a test application and can confirm the columns in a group are not cleared by Columns.Clear when used on its own.
2) I'm populating the grid manually because (a) I like to be in control <smile> and secondly (b) I load the data for the grid using a background worker thread to prevent the user interface freezing and allowing me to show a progress bar during fetching and loading. I realise I can use virtual mode to achieve some of these things, but prefer not to.
3) The problem only manifests itself when the Refresh button is in a seperate container control to the grid. I know this is hard to believe, but I have a test project which shows this clearly. In my "live" application the refresh button is within a RibbonBar and the grid is in a DocumentWindow, but I can show the same thing happening in my test project by putting the grid and the refresh button in seperate DocumentWindows on the same RadDock control.

0
Phillip
Top achievements
Rank 1
answered on 18 Aug 2009, 04:07 PM
Victor,

I have trimmed down my sample to the minimum possible code. To repro;
Place a RadDock on a Windows Form. Add two DocumentWindows (I put mine one above the other in two TabStrips so I can see both at once).
Place a RadButton (radButton1) on documentWindow1.
Place a RadGridView (radGridView1) on documentWindow2.

Put the following code at form level.

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.buildColumns();
            this.buildRows();
        }

        private void buildRows()
        {
            // Add 10 rows
            for (int count = 0; count < 10; count++)
            {
                this.radGridView1.Rows.Add( count.ToString());
            }
        }

        private void buildColumns()
        {
            Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn1 = new Telerik.WinControls.UI.GridViewTextBoxColumn();
            gridViewTextBoxColumn1.HeaderText = "column1";
            gridViewTextBoxColumn1.UniqueName = "column1";
            this.radGridView1.Columns.Add(gridViewTextBoxColumn1);
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            // Refresh grid.
            this.radGridView1.Rows.Clear();
            this.radGridView1.Columns.Clear();
            this.buildColumns();
            this.buildRows();
        }

When the grid is initially shown, edit some of the text boxes. The press the radButton to refresh the grid. The code falls over on "this.radGridView1.Rows.Clear()" with an internal error. This is what I see in my live application.

Hope this helps.

0
Victor
Telerik team
answered on 20 Aug 2009, 11:19 AM
Hello Phillip,

I am afraid I still cannot reproduce the issue you described. I followed your descriptions meticulously and it did not throw a null reference exception. Moreover, the columns and rows are cleared as expected (please see the attached screenshots). Probably I am not testing with the correct version of the controls, currently I am using the latest one (the latest build we have here).

The support ticket indicates that you are using Q1 2009 SP1, however, this can not be correct because Q1 does not include RadDock. Which version are you using so that I can test the application further? I am looking forward to your reply.

Sincerely yours,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Phillip
Top achievements
Rank 1
answered on 20 Aug 2009, 12:54 PM
Victor,
I am using Q2 2009. I can upload my test application for you if you like ? How do I upload (I can't see an obvious upload or attach button anywhere in the forum page).
I'm out of the office next week, so I'd like to upload it today or tomorrow if possible.

Phillip
0
Victor
Telerik team
answered on 21 Aug 2009, 06:49 AM
Hi Phillip,

Thank you for writing. As I thought, the issue is present in Q2 2009 release and has been addressed in Q2 2009 Service Pack 1. You cannot attach files in the forum. You need to submit a support ticket. You can do this by logging in your account at Telerik.com and navigating to Your Support --> Support Tickets.
Write back if you have questions or if you need assistance with our products.

Regards,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Phillip
Top achievements
Rank 1
answered on 21 Aug 2009, 10:28 AM
Victor,

Its good to know you can reproduce the fault. When is Q2 2009 SP1 due to be released please ?
0
Accepted
Victor
Telerik team
answered on 21 Aug 2009, 11:00 AM
Hi Phillip,

Q2 2009 SP1 was released almost a month ago. You can download the installer from our website. Write again if you have more questions.

All the best,
Victor
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
GridView
Asked by
Phillip
Top achievements
Rank 1
Answers by
Victor
Telerik team
Phillip
Top achievements
Rank 1
Share this question
or