I have a problem sorting columns in a radgrid, with null objects in it.
The initial binding is all fine, and no problems.Not untill I try to sort a column with null objects in it.
I programmaticly create the columns like:
GridBoundColumn boundColumn = new GridBoundColumn();
view.Columns.Add(boundColumn);
boundColumn.DataField = "My.Custom.Object"; (note that Custom willbe null at some times, and therefore we cant sort the column).
The error occurs when I rebind the grid in Page_PreRender.
RadGrid1.DataSource = SomeDataSource;
try
{
RadGrid1.Rebind();
}
catch (Exception e)
{
// We will have a "Object reference not set to an instance of an object" error here...
}
Any way to get around this error?
Regards
Andreas Clausen
5 Answers, 1 is accepted
I am not sure whether this suggestion will work or not. But you may try the following code snippet in the ItemDataBound event to replace the cell having null value with and see whether Sorting of the GridBoundColumn is working.
CS:
| protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) |
| { |
| if (e.Item is GridDataItem) |
| { |
| GridDataItem item = (GridDataItem)e.Item; |
| if (item["colUniqueName"].Text == null) |
| { |
| item["colUniqueName"].Text = " "; |
| } |
| } |
| } |
Shinu
Hi Shinu,
If I loop through all the dataitems in the _ItemDataBound event, no items (.Text) has null values.
They will have as default, and in my case they will be "--" (boundColumn.EmptyDataText = "--").
So unfortunately nothing will change (I tried) with the code you pasted.
Strange thing is that the bind itself, works fine with nullable objects, but the sorting does not :S
Any other ideas/workarounds?
Regards
Andreas Clausen
Scenario is (thought):
DataField
="House.Door.Size" <- (We have a problem when sorting by this, and the house has no doors (House.Door is null) )
// Andreas Clausen
Can you please check whether the same issue appear when binding your custom collection to regular MS GridView control? Furthermore, in case you use the .NET 3.5 version of the grid, does setting EnableLinqExpressions = false for the control addresses the error in question?
Let us know what your findings are.
Kind regards,
Sebastian
the Telerik team
Check out Telerik Trainer , the state of the art learning tool for Telerik products.
Setting the property EnableLinqExpressions = False, actually did the trick.
Was doing alot of tests on both yours and MS gridview, but never got it up and working.
Guess you saved me from nearly going insane :)
Thanks alot for your help on this one.
// Andreas Clausen