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

Children element label NullReferenceException

12 Answers 144 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Alan
Top achievements
Rank 1
Alan asked on 14 Apr 2009, 12:33 PM
An error occurs in the cellFormating event:

in the first databinding of gridview the error does not happen , but if I try  a second databinding, for refresh the gridview,
the following error occurs:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.WinControls.Primitives.TextPrimitive.MeasureOverride(SizeF availableSize)

The code where the error occurs is the folowing:

 private void gvPremios_CellFormatting(object sender, CellFormattingEventArgs e)  
                         {  
 if (e.CellElement.ColumnInfo is GridViewDataColumn && !(e.CellElement.RowElement is GridTableHeaderRowElement))  
                             {  
                                     GridViewDataColumn column = (GridViewDataColumn)e.CellElement.ColumnInfo;  
 
                                     if (column.DataField == "MyColumn")  
                                     {                         
                                         if (e.CellElement.Children.Count > 0)  
                                             return;  
 
                                         RadLabelElement element = new RadLabelElement();  
                                         element.Text = "<html>This is a test..."                                           
                                        e.CellElement.Children.Add(element);  
                                     }  

The strange thing is in the "element.text" property, that if I not put the the tag "<html>"... the error does not happen, but, i need formating the field with a html code...
And the property "DisableHTMLRendering=false"  is not working as it should, leaving the whole column unconfigured.

12 Answers, 1 is accepted

Sort by
0
Alan
Top achievements
Rank 1
answered on 15 Apr 2009, 02:07 AM
Somebody help-me?
0
gerbrand
Top achievements
Rank 2
answered on 15 Apr 2009, 08:46 AM
Hi Alan,

On which line is the error triggered?




0
Nikolay
Telerik team
answered on 15 Apr 2009, 09:41 AM
Hi Alan,

You can find the answer in your support ticket regarding the same topic.

Greetings,
Nikolay
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
Alan
Top achievements
Rank 1
answered on 15 Apr 2009, 12:24 PM
Hi,

there is no new response in the ticket system.
the property "DisableHTMLRendering=false"  is not working as it should, leaving the whole column unconfigured.


gerbrand
The error occurs after leaving the CellFormating event, and only in the second databinding of the gridview.
but if, i not put the "<html>" tag in the element.Text, the error does not happen.
there is a way to reset the gridview, let him clean before the next databinding, or some other solution?


thanks for attention.
0
gerbrand
Top achievements
Rank 2
answered on 15 Apr 2009, 01:23 PM
Hi Alan,

You can clean your gridview like this:

radgridview.MasterGridViewTemplate.Columns.Clear(); //clears the columns 
radgridview.MasterGridViewTemplate.Rows.Clear(); //clears the rows 
radgridview.MasterGridViewTemplate.ChildGridViewTemplates.Clear(); //clears the childs 

I'm binding my data in my code and not by datasource, so I'm not getting this error. I'm not sure how you are doing the databinding?
(by datasource?)







0
Alan
Top achievements
Rank 1
answered on 15 Apr 2009, 01:49 PM
Hi gerbrand,

I clean the childs and the rows, but the error still happening.

I'm binding using the follow code:

Gridview.MasterGridViewTemplate.LoadFrom(IDatareader); 

but have also tested it with datasource...

you can show me as it did in your code?
so I compare with my code.

Thanks for your help.
0
Nikolay
Telerik team
answered on 15 Apr 2009, 02:10 PM

Hi Alan,

You have opened two inquiries with the same question. Since support tickets get higher priority, we replied there first. The next day we noticed that you have the same question posted in the forums - this is why we replied to look into your support question.

We would also prefer to continue addressing this question in just one thread - this will make sure that we have all information in one place, rather than jumping back and forth between different threads. This will make us respond faster and more accurately.

Thank you for your understanding. We will reply in this thread shortly.

Kind regards,

Nikolay
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
gerbrand
Top achievements
Rank 2
answered on 15 Apr 2009, 02:46 PM
Hi Alan,

Well I've got an List<Object> (where Object is a collecion of getters and setters) that I'm filling up first.
(because I need data from more than one table )
After that I'm looping my List<Object> and set my griddata like described in the helpfiles of the RadGridView

Gridview.MasterGridViewTemplate.Rows.Add(new object[] { "Another new product", 456 }); 



I can give you a more detailed example if you want...


0
Alan
Top achievements
Rank 1
answered on 15 Apr 2009, 03:11 PM
Hello, gerbrand

Yes, I would be grateful if you could please pass details of your code.

if possible an example of how to perform the binding with a list of objects, i will try it this way.

Thanks again.
0
gerbrand
Top achievements
Rank 2
answered on 15 Apr 2009, 04:26 PM
Hi Alan,

Here is an example of how I do it.

//clear the gridview of everything 
rgvNetwork.MasterGridViewTemplate.Columns.Clear(); 
radGridView.MasterGridViewTemplate.Rows.Clear(); 
radGridView.MasterGridViewTemplate.ChildGridViewTemplates.Clear(); 
 
 
//add the columns for the master 
radGridView.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Id", HeaderText = "Name", HeaderTextAlignment = ContentAlignment.MiddleCenter }); 
radGridView.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Name", HeaderText = "Adserver Id", HeaderTextAlignment = ContentAlignment.MiddleCenter }); 
radGridView.MasterGridViewTemplate.Columns.Add(new GridViewDataColumn { FieldName = "Active", HeaderText = "Active", HeaderTextAlignment = ContentAlignment.MiddleCenter }); 
 
//add the columns for the childs 
var template = new GridViewTemplate { AllowAddNewRow = false, AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill }; 
template.Columns.Add(new GridViewTextBoxColumn { FieldName = "Id", IsVisible = false }); 
template.Columns.Add(new GridViewTextBoxColumn { FieldName = "IdChild", HeaderText = "Zone Name", HeaderTextAlignment = ContentAlignment.MiddleCenter}); 
template.Columns.Add(new GridViewTextBoxColumn { FieldName = "NameChild", HeaderText = "Zone Id", HeaderTextAlignment = ContentAlignment.MiddleCenter}); 
 
//add the childtemplate to the master 
radGridView.MasterGridViewTemplate.ChildGridViewTemplates.Add(template); 
 
//make the relationship between master and child 
var relation = new GridViewRelation(radGridView.MasterGridViewTemplate) 
            { 
                ChildTemplate = template, 
                RelationName = "SiteZonesNetwork" 
            }; 
 
relation.ParentColumnNames.Add("Id"); //equals the fieldname 
relation.ChildColumnNames.Add("Id"); //equals the fieldname 
 
//add the relation 
radGridView.Relations.Add(relation); 
 
 
//load your List<Master> 
var myListMaster = new List<Master>(); 
 
var child1 = new List<Child>(); 
child1.Add(new Child{ Id = 2, Name = "ChildTest2" }); 
child1.Add(new Child{ Id = 3, Name = "ChildTest3" }); 
 
var child2 = new List<Child>(); 
child1.Add(new Child{ Id = 5, Name = "ChildTest5" }); 
child1.Add(new Child{ Id = 6, Name = "ChildTest6" }); 
child1.Add(new Child{ Id = 7, Name = "ChildTest7" }); 
 
myListMaster.Add(new Master { Id = 1, Name = "Test", Childs = child1 }); 
myListMaster.Add(new Master { Id = 4, Name = "Test2", Childs = child2 });                         
 
 
//fill the radgridview 
foreach(var myMaster in myListMaster){ 
 
  radGridView.MasterGridViewTemplate.Rows.Add(new object[] { myMaster.Id, myMaster.Name, myMaster.Active }); 
  template = radGridView.MasterGridViewTemplate.ChildGridViewTemplates[0]; 
   
  foreach(var child in myMaster.childs){ 
   
    template.Rows.Add(new object[] { myMaster.Id, child.Id, child.Name }); 
  } 
   

Where my 2 objects are written like this:
public class Master{ 
  public int Id {get;set;} 
  public string Name {get;set;} 
  public List<Child> Childs{get;set;} 
 
public class Child{ 
  public int Id{get;set;} 
  public string Name {get;set;} 


It's possible that I have typo's in the example code. I wrote it quickly in notepad. But I think you will get the idea.


0
Alan
Top achievements
Rank 1
answered on 16 Apr 2009, 02:45 AM
Thank you very much for helping me.
0
Nikolay
Telerik team
answered on 22 Apr 2009, 03:49 PM
Hi Alan,

You can find the answer to your question about the HTML rendering in your support ticket.

gerbrand, thank you for your community efforts. I have updated your Telerik points.

Kind regards,
Nikolay
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
Alan
Top achievements
Rank 1
Answers by
Alan
Top achievements
Rank 1
gerbrand
Top achievements
Rank 2
Nikolay
Telerik team
Share this question
or