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

What is the CORRECT way to handle null values in a RadGrid Column...

1 Answer 681 Views
Grid
This is a migrated thread and some comments may be shown as answers.
mark baer
Top achievements
Rank 1
mark baer asked on 29 Jun 2011, 12:48 AM
I am using WCF Dataservices so I have hierarchical data...Some of my values are coming back with null and I am getting "Input String was not in a correct format"...I am trying to handle the null values in the radgrid_ItemDatabound, but I am still having problems...How SHOULD I be handling null values in the radgrid column. 

RadGrid HTML:
<telerik:RadGrid ID="radgridCredits" runat="server" 
AllowFilteringByColumn="True"<BR>                                                
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
CellSpacing="0"<BR>                                                
GridLines="None" Height="270px" 
OnNeedDataSource="radgridCredits_NeedDataSource"<BR>                                                
OnSelectedIndexChanged="radgridCredits_SelectedIndexChanged" 
OnItemDataBound="radgridCredits_ItemDatabound" PageSize="100" 
ShowGroupPanel="True"<BR>                                                 
Visible="False" 
Width="100%"><BR>                                                
<ClientSettings 
AllowDragToGroup="True"><BR>                                                    
<Scrolling AllowScroll="True" UseStaticHeaders="True" 
/><BR>                                                
</ClientSettings><BR>                                                
<MasterTableView><BR>                                                    
<CommandItemSettings ExportToPdfText="Export to Pdf" 
/><BR>                                                    
<Columns><BR>                                                        
<telerik:GridButtonColumn CommandName="Select" DataTextField="Show.Title" 
FilterControlAltText="Filter column2 
column"<BR>                                                            
HeaderText="Show" SortExpression="Show.Title" 
UniqueName="column2"><BR>                                                            
<HeaderStyle Width="25%" 
/><BR>                                                            
<ItemStyle Width="25%" VerticalAlign="Top" 
/><BR>                                                        
</telerik:GridButtonColumn><BR>                                                        
<telerik:GridBoundColumn DataField="Show.Medium.name" 
FilterControlAltText="Filter TalentColumn 
column"<BR>                                                            
FilterControlWidth="50px" HeaderText="Media" 
SortExpression="Show.Medium.name"<BR>                                                            
UniqueName="TalentColumn"><BR>                                                            
<HeaderStyle Width="9%" 
/><BR>                                                            
<ItemStyle VerticalAlign="Top" Width="9%" 
/><BR>                                                        
</telerik:GridBoundColumn><BR>                                                        
<telerik:GridBoundColumn DataField="Craft.name" FilterControlAltText="Filter 
column1 
column"<BR>                                                            
FilterControlWidth="75px" HeaderText="Craft" SortExpression="Craft.name" 
UniqueName="column1"><BR>                                                            
<HeaderStyle Width="15%" 
/><BR>                                                            
<ItemStyle Width="15%" VerticalAlign="Top" 
/><BR>                                                        
</telerik:GridBoundColumn><BR>                                                        
<telerik:GridBoundColumn DataField="Show.Project.us_bor" 
DataFormatString="${C:0}" FilterControlAltText="Box 
Office"<BR>                                                            
FilterControlWidth="50px" HeaderText="US BO(US$)"  
SortExpression="Show.Project.us_bor"<BR>                                                            
UniqueName="colBoxOffice"><BR>                                                            
<HeaderStyle Width="8%" 
/><BR>                                                            
<ItemStyle Width="8%" VerticalAlign="Top" 
/><BR>                                                        
</telerik:GridBoundColumn><BR>                                                        
<telerik:GridBoundColumn DataField="Show.Project.ProdHouse.name" 
FilterControlAltText="Filter column3 
column"<BR>                                                            
HeaderText="Production House" SortExpression="Show.Project.ProdHouse.name" 
UniqueName="column3"><BR>                                                            
<HeaderStyle Width="12%" 
/><BR>                                                            
<ItemStyle Width="12%" VerticalAlign="Top" 
/><BR>                                                        
</telerik:GridBoundColumn><BR>                                                        
<telerik:GridBoundColumn DataField="comment" FilterControlAltText="Filter 
column4 
column"<BR>                                                            
HeaderText="Comment" SortExpression="comment" 
UniqueName="colComment"><BR>                                                            
<HeaderStyle Width="22%" 
/><BR>                                                            
<ItemStyle Width="22%" VerticalAlign="Top" 
/><BR>                                                        
</telerik:GridBoundColumn><BR>                                                        
<telerik:GridBoundColumn DataField="Show.year" FilterControlAltText="Filter 
column5 
column"<BR>                                                            
FilterControlWidth="40px" HeaderText="Year" SortExpression="Show.year" 
UniqueName="column5"><BR>                                                            
<ItemStyle VerticalAlign="Top" 
/><BR>                                                        
</telerik:GridBoundColumn><BR>                                                    
</Columns><BR>                                                    
<EditFormSettings><BR>                                                        
<EditColumn FilterControlAltText="Filter EditCommandColumn 
column"><BR>                                                        
</EditColumn><BR>                                                    
</EditFormSettings><BR>                                                
</MasterTableView><BR>                                                
<FilterMenu 
EnableImageSprites="False"><BR>                                                
</FilterMenu><BR>                                                
<HeaderContextMenu CssClass="GridContextMenu 
GridContextMenu_Default"><BR>                                                
</HeaderContextMenu><BR>                                            
</telerik:RadGrid>

Here is my item databound event...
<P> protected void radgridCredits_ItemDatabound(object sender, 
GridItemEventArgs e)<BR>        
{<BR>            
try<BR>            
{<BR>                
if ((e.Item.ItemType == Telerik.Web.UI.GridItemType.Item) 
||<BR>                   
(e.Item.ItemType == 
Telerik.Web.UI.GridItemType.AlternatingItem))<BR>                
{<BR>                    
if (e.Item is 
GridDataItem)<BR>                    
{<BR>                        
GridDataItem gridDataItem = 
(GridDataItem)e.Item;<BR>                        
Credit credit = (Credit)(e.Item.DataItem);</P>
<P>                        
if (!credit.Show.title.ToLower().Contains("select a 
talent"))<BR>                        
{<BR>                            
if (credit.comment == 
null)<BR>                            
{<BR>                                
gridDataItem["colComment"].Text = 
"";<BR>                            
}<BR>                            
if (credit.Show.Project.us_bor == 
null)<BR>                            
{<BR>                                
gridDataItem["colBoxOffice"].Text = 
"";<BR>                            
}<BR>                        
}<BR>                    
}  </P>
<P>                   
// Telerik.Web.UI.GridDataItem item = e.Item as 
Telerik.Web.UI.GridDataItem;<BR>                    
//Credit credit = 
(Credit)e.Item.DataItem;<BR>                    
//if 
(credit.comment==null)<BR>                    
//{<BR>                    
//    credit.comment = 
"";<BR>                    
//}<BR>                    
//if (credit.Show.Project.us_bor == 
null)<BR>                    
//{<BR>                    
//    credit.Show.Project.us_bor = 
0m;<BR>                    
//}<BR>                    
//Get the "colDivision" cell value using the columnUniqueName.  
<BR>                    
//if (item["comment"].Text == 
null)<BR>                    
//{<BR>                    
//    item["comment"].Text = 
"";<BR>                    
//}<BR>                
}  
<BR>               
<BR>            
}<BR>            catch 
(Exception 
ex)<BR>            
{<BR>                
SiteMaster masterPage = 
(SiteMaster)Page.Master;<BR>                
masterPage.SetAlertMessage("Error fetching Talent Credits:  "
ex.Message);<BR>                
return;<BR>            
}<BR>        }</P><BR>

1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 29 Jun 2011, 10:41 AM
Hello,

 Normally the grid can handle null value in the columns. The problem might be due to the fact that you are binding to properties of nested objects (Show.Project.ProdHouse.name, Show.Project.us_bor etc.) So if any of those nested objects are null an exception might be thrown. You should make sure that you always bind to objects that have non-null values for the nested objects in order to access the most inner property. For example you might filter the null records before passing them to a datasource for the grid. Otherwise if for example Project is null the grid will try to access Show.Project.us_bor which will cause an exception.

Best wishes,
Marin
the Telerik team

Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

Tags
Grid
Asked by
mark baer
Top achievements
Rank 1
Answers by
Marin
Telerik team
Share this question
or