Wednesday, March 10, 2010

A useful post for Telerik Users?

Well I found out, that these Telerik WinControls are boon and bane. I like theme because I do a lot of great design stuff via XML and Themes. And if you know what you’re doing these controls really perform. But I ran into a System.StackoverflowException and I found the documentation on that specific error very helpful. So thx anyway Microsoft. If I wouldn’t knew it anyway - what caused that error - I guess I would be lost. Take a moment, before they change the documentation on this error, I make a screenshot.

image

I like the explanation part.

image

Saying: A StackOverflowException occurs when the Stack overflows. Yes, who’d a thunk? Or WTF? So guys what happened here?

Well looks like the beloved Telerik.RadComboBox had a little event handling problem calling the Data Source. Since it's a bindingproblem within the RadControl there is no way for me changing that. Since the Data comes from a Service i also had no chance to edit that. Anyway debugging that is not easy. Found a cool article on this topic. Where? Yes, at stackoverflow.com! Since I have a modular software and want to fill the items of the comboBox by a public data Method and some parts at the control itself, it turns out the question: “How can I do that?”

So first I make sure evrything works fine when the controls are called. And check if the data source is null. In my example I want to bind the German tax authorities to that Combobox.

  private void InitMyComboBox()
 {
   ...
   if (comboBoxTaxAuthority.DataSource == null)
       comboBoxTaxAuthority.DataSource =
                            Logic.GetTaxAuthorities();
   ...
 }

“Logic” is a Mapper which points me as you might think to a static implementation of a Singleton which loads that data from a service.

Next step is to implement the ItemDataBound event.

private void ComboBoxTaxAuthorityItemDataBound(object sender, ItemDataBoundEventArgs e)
{
   var item = e.DataBoundItem as RadComboBoxItem;
   if (item != null)
   {
     var x = e.DataItem as TaxAuthority;
     if (x!=null)
     {
       item.Value = x;
       item.Text = x.Name;
       item.DescriptionText = "Finanzamt " + x.Name;
       item.Image = Properties.Resources.authorities;
       item.TextImageRelation =
                     TextImageRelation.ImageBeforeText;
      }
    }
}

So what this does, it casts the DataBoundItem which I get from the method to a RadComboBoxItem so that I can manipulate it at design time and set the things how they should be displayed. in the next step I set the properties. For example I want to set the Text and Value of the new item to it’s inner DataBoundItem properties. And then I can set the Image from the Resource file to appear in the ComboBox. The last thing to do is to set the Image Text relation of the object. So this is the way how I solved the problem with the databinding and image from a resource file. Doing it by hand however is not the fastest way but i get rid of that stupid exception. It would take much longer to show the code which caused that error. Because the hole class is about 150.000 Lines of code. To cut the edges I just wanna mention 2 things. First: Don’t program Event Driven. The idea of event driven development is totally wrong when you interact with nearly any sort of communication or data interaction. Think about the latency between “Event is fired” and “the whole Data is loaded and send to the control”. You can call that Message Driven. So thinking "message driven" instead of "event driven" helps you understanding how the system works. Get rid of the PEBKAC (problem exist between keyboard and chair) So disable your controls while waiting for the Message! The result looks like that:

image

So i just have to do the Error Providers and the localization stuff, and yes the Description Text should be reviewed.

No comments: