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

Editing a ComboBox column cell in which the column is being sorted causes NullReferenceException

5 Answers 65 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dexter
Top achievements
Rank 1
Dexter asked on 25 Jul 2014, 05:11 PM
Hello, I would like to enquire on a solution of the problem as per title.
To elaborate, let's have 2 column as example: Serial and Status, where Serial is a regular data column, while Status is a ComboBox column.
If I sort the GridView by Status column (clicking the Status header), and I tried to edit a Status cell, I will get NullReferenceException. Debugging shows that RadGridView.SelectedItem is null.
I have no problem with other scenarios, such as sorting by Status column and edit a Serial cell, or sorting by Serial column and edit a Status cell, or sorting by Serial column and edit a Serial cell.

Thanks a lot for your attention.

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 28 Jul 2014, 11:39 AM
Hi,

We fixed a null reference exception with RadComboBox recently. May I ask you to test with the latest version (latest internal build)?

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Dexter
Top achievements
Rank 1
answered on 08 Aug 2014, 09:27 PM
Hello, I have revisited this problem, and it still exists. Having a look at the changelog, it doesn't show any fixes related to what I'm encountering. I have upgraded 2014 Q2 SP1.

For more info:
My ComboBoxColumn values are binded. Unlike editing any other column, editing a ComboBoxColumn triggers the "OnSelectionChanged" event of the GridView. Is it meant to be like this? The NullReferenceException occurs when I call upon "Gridview.SelectedItem" in the same event handler, where GridView is the one which the ComboBoxColumn belongs to which I'm editing. It shows GridView.SelectedItem is null. Do not understand why this is so as well - since I'm already editing it, it has to be selected.

So in summary, 2 things:
Why is SelectionChanged event being fired when editing a ComboBoxColumn?
It wouldn't have been a problem too actually, but GridView.SelectedItem is null, which contradicts that SelectionChanged is being fired. 


0
Dimitrina
Telerik team
answered on 11 Aug 2014, 08:30 AM
Hi,

I tested editing a ComboBoxColumn. As I subscribed for the RadGridView.SelectionChanged event, it was raised only the first time when I clicked on a row to start editing, no matter if I started editing the ComboBoxColumn or a GridViewDataColumn. After it was raised initially and the row was selected, I can edit the cells and the event is not raised any more.

If gridView.SelectedItem is null, this means the SelectedItem is still assigned for some reason. In my tests this value was always presented. May I ask you to share more details on your exact setup and why do you need to access it?  

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Dexter
Top achievements
Rank 1
answered on 11 Aug 2014, 09:12 AM
Ok sorry about the confusion.
After more experimenting, I found out that this actually happens to all kinds of columns, not just ComboBoxColumn.
The SelectionChanged event will basically fire whenever editing the row causes the row to change position due to one of the columns is being sorted, be it ascending or descending. Within this, .SelectedItem = null.
0
Dimitrina
Telerik team
answered on 11 Aug 2014, 12:26 PM
Hello,

Thank you for sharing those additional notes. I was able to reproduce the issue.

The reason why the SelectedItem is null is that the SelectionChanged method is raised because of removing the selection of the row while resorting the items. Basically the SelectionChanged will be raised when you select an item and when you deselect an item. 
In this case you should skip your actions when the SelectedItem is null and wait for a notification when an item is actually selected.

You can check the code snippet below to better understand my explanation:
private void pilotsGrid_SelectionChanged(object sender, SelectionChangeEventArgs e)
{
    var grid = sender as RadGridView;
    string addedRow = string.Empty;
    string removedRow = string.Empty;
    if (e.AddedItems.Count > 0)
    {
        addedRow = (e.AddedItems[0] as Pilot).FirstName;
        MessageBox.Show(string.Format("Selected item: {0}, Selected in args: {1}", (grid.SelectedItem as Pilot).FirstName, addedRow));
    }
    else if (e.RemovedItems.Count > 0)
    {
        removedRow = (e.RemovedItems[0] as Pilot).FirstName;
        MessageBox.Show(string.Format("Removed item: {0}",  removedRow));
    }
}

Regards,
Didie
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Dexter
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Dexter
Top achievements
Rank 1
Share this question
or