Jaa


Databinding in a UserControl vs. WinForm

Alright, I answered another question in a newsgroup, but the question posed, while technical, gets an A+ for honesty. It starts like this:

I'm tearing my hair out at the moment trying to work out why the above
isn't working!

Now, the "above" is that a custom UserControl in a VSTO task pane does not do databinding as expected. It's easy to repro, and it can be repro'd in a simple WinForm app. So, I dedicate this blog entry to Keni (the ng poster). Get your Minoxidil, and here are the steps:

  1. Create a WinForm app
  2. Add new custom control
  3. Add a new datasource (Northwind's Regions table is fine)
  4. Drag the table form the Datasource window onto the custom control. A datagrid should appear on it.
  5. Build
  6. Drag custom control from toolbox to your startup WinForm
  7. F5

What you will notice is that the custom control's grid never fills. I initially thought this was a bug, because if you follow the same steps, but instead of adding a custom control and adding the grid to that control, just add the grid directly on the WinForm and F5. The grid will fill.

Well, it's by design. The reason actually does make sense. There was an internal bug filed on this earlier this year, but it was resolved as "by design". Here's the skinny:

It all comes down to complexities that would arise if we had the code automatically call the fill method of the table's adaptor when the control loads. These complexities under-the-hood do not present themselves in the much simpler arrangement of straight-forward WinForm databinding. To avoid these, we do not put the code in there to call the Fill method. You should add your own custom procedure to fill the dataset to your control. Call that method from an event in the host form or elsewhere in the app. Do not fill the dataset when the control loads.

For example, here's my simple procedure to fill the dataset:

Public Sub FillDS()

Me.RegionTableAdapter.Fill(Me.RegionDataSet._Region)

End Sub

Here's the code I use to call this procedure in the control:

Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

  Me.UserControl11.FillDS()

End Sub

This post was on the microsoft.public.vsnet.vstools.office news group.

Rock Thought for the Day: Heard Killling Joke's song 'Wardance" off their 2003 self-titled CD. Folks, if Green Day is your best connection to punk rock, uh...it's time to dig a little deeper. Iggy Pop's "Lust for Life", anything by the Ramones, and the Clash's "White Riot" just for starters. I caught onto these songs in the seventies as a kid, and they reached my soul.

Rock On

Comments

  • Anonymous
    September 01, 2005
    What about Black Flag?? One of the best and most influential punk rock bands ever.
  • Anonymous
    September 02, 2005
    I agree about Black Flag. I have blogged about them this year in my Rock Thought. They broke ground, and their songs still hold up.

    Nice catch, Mark!
  • Anonymous
    September 02, 2005
    hey John, Wardance in 2003? The song comes from Killing Joke's self-titled debut in 1980. But you have (finally) touched on a fantastic band with one of the hardest-working leaders in Jaz Coleman. They have something like 20 studio albums, and, while not for every consumer, have influenced many other bands. They made one attempt at the mainstream -- 1985's Night Time -- but have otherwise created some powerful punk/rock/techno sounds. Amazing to me how Jaz can get through an entire set shredding his vocal chords like that...
  • Anonymous
    September 02, 2005
    Um, sorry for the jab at your music tastes....you've talked about many great bands. You know me...I'm just a new wave/indie music snob. ;-)
  • Anonymous
    September 06, 2005
    No problem,Christian. I noted that the album was 2003 because it was a re-issue. I should have called that out more carefully.

    Good catch!!