June 2004 - Posts - Pann's Thoughts

June 2004 - Posts

If someone knows of an easy way of submitting code in a blog, please let me know.
Currently it's a bit of a pain.
Posted by roaan

Use the "Binding.Format"
and "Binding.Parse" events to your advantage.

E.g.

Let's say you want to use databinding on a checkbox. Typically the checkbox
understands true/false and 1/0. But lets say the database designer in his/her wisdom decided to
store "Y"/"N" in the database instead of the 1/0. With default databinding you'll have a problem
since the default mechanisms can't convert "Y" or "N" to a boolean.

I've seen some interesting (non working) solutions to this problem, the most common being deriving
from CheckBox and redefining "Checked" (i.e. public new object Checked). In my experience this has so
far not worked that well, besides which, there is a built in solution. That's right, using Binding.Format and Binding.Parse.

Binding.Format - This event gets fired just after the data has been read from the data source, and
before it populates the control.

Binding.Parse - This event gets fired after the data has been read from the control, and just before it
gets written to the datasource.

These events allow us to change the data as it is written/read from the source and read/written to
the control.

Following is a short code snippet to illustrate.

First we write the Format event listener. It will convert data from the datasource to a format
the control can understand.

private void ConvertToBool(object sender, ConvertEventArgs e)
{
   if (e.Value is String)
   {
      if (e.Value.Equals( "Y"))
      {
         e.Value= true;
      }
      else
      {
         e.Value= false;
      }
   }
}

Next we write the Parse event listener. This will convert the data from the control to a format the
datasource can understand

private void ConvertToString(object sender, ConvertEventArgs e)
{
   if ((e.Value is bool) && (e.DesiredType == typeof( string)))
   {
      bool isTrue= (bool)e.Value;
      if (isTrue)
      {
         e.Value= "Y";
      }
      else
      {
         e.Value= "N";
      }
   }
}

All that's left now is to do the actual databinding and hook up the events

private void DoBinding ()
{   
   DataTable table= GetDataSource();
   Binding newBinding= new Binding( "Checked", table, "MustCheck");  // Must check is the column in my datasource   
   newBinding.Format+=new ConvertEventHandler(ConvertToBool);
   newBinding.Parse+= new ConvertEventHandler(ConvertToString);
   m_chkTest.DataBindings.Add( newBinding);                                // m_chkTest is my checkbox
}

 

 

 

Posted by roaan | 2 comment(s)
I think I've got BlogJet and its images working. Let's see
Posted by roaan

I'm testing 2 items with this post.

1) Matt told me how to set the time zone so that the post time is correct. I'm gonna test it out

2) I'm testing a new blog posting tool BlogJet. I've been using the NewsGator plugin, but it seems I can do a lot more with BlogJet

Posted by roaan