none
Binding a control array to a dataset RRS feed

  • Question

  • I have an array of CheckBox controls I am using.  Each control is repeated 9 times.  I am trying to bind these controls to a dataset obtained from a database query.  In its simpest form, this is the code that I am trying to use:

    ds.Clear()
    da.Fill(ds, "Data")

    For i = 0 to 8

     chkArray(i).DataBindings.Clear()
     chkArray(i).DataBindings.Add( _
          New Binding("Checked", ds.Tables("Data").Rows(i), "bolCheckbox"))

    Next

    bolCheckBox is a valid column header in the database.  When I run the code, I get an argument exception: "Cannot bind to the property or column bolCheckBox on the DataSource.
    Parameter name: dataMember".

    Using the same SQL query, I can bind the same dataset successfully to a DataGridView using:

    DataGridView1.DataSource = ds.Tables("Data")

    What am I missing about binding to the individual controls in the control array?

    Thanks.

    Friday, January 21, 2011 10:39 PM

Answers

  • You just can't do this without using a list type of control, such as a DataGridView. Here's a simplistic explanation:

    The list type of controls have complex binding (meaning they bind to both columns *and* rows), but controls like CheckBox and TextBox have simple binding, meaning they bind just to columns and rely on the BindingContext's/CurrencyManager's postion to determine the row that gets displayed.  That's simply how databinding works.

    I think you're stuck using a DGV. =0(


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by JohnZonie Saturday, January 22, 2011 6:36 PM
    Saturday, January 22, 2011 6:16 PM

All replies

  • DataBinding won't work that way ... you are trying to bind 9 different rows in your DataTable. The typical DataBinding statement looks like this (note that I removed the .Rows(i)):

    chkArray(i).DataBindings.Add( _ 
       New Binding("Checked", ds.Tables("Data"), "bolCheckbox"))
    
    ' or, a simpler syntax:
    chkArray(i).DataBindings.Add( _ 
       "Checked", ds.Tables("Data"), "bolCheckbox")
    
    

    You don't specify Rows in the DataBinding, that is handled by the BindingContext (aka CurrencyManager). What you actually want to do is use a CheckedListBox rather than an array of CheckBoxes.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, January 22, 2011 4:30 PM
  • Thanks, Bonnie.  I see I need to study the CurrencyManager topic more.  I tried something quickly but it put the same data in all 9 checkboxes.  I tried

    cm = CType(Me.BindingContext(ds), CurrencyManager)

    and in the for/next loop, I added a cm.position

    For i = 0 to 8
     cm.Position = i
     chkArray(i).DataBindings.Clear()
     chkArray(i).DataBindings.Add( _ 
       New Binding("Checked", ds.Tables("Data").Rows(i), "bolCheckbox"))
    
    Next
    
    

    This simply gave me the same value for all 9 controls.

    (Actually I have a number of control arrays I am trying to bind to the database - CheckBoxes, ComboBoxs (SelectedIndex only), NumericUpDowns and TextBoxes.  The above example is a minimal case.  If I can get that going, everything else should follow.  Currently I successfully bind ds to an invisible DGV and then through an ugly for/next loop set the control array values to the corresponding DGV cell.  I am trying to eliminate the middle man - the DGV.)

    Thanks again!

    Saturday, January 22, 2011 6:01 PM
  • You just can't do this without using a list type of control, such as a DataGridView. Here's a simplistic explanation:

    The list type of controls have complex binding (meaning they bind to both columns *and* rows), but controls like CheckBox and TextBox have simple binding, meaning they bind just to columns and rely on the BindingContext's/CurrencyManager's postion to determine the row that gets displayed.  That's simply how databinding works.

    I think you're stuck using a DGV. =0(


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by JohnZonie Saturday, January 22, 2011 6:36 PM
    Saturday, January 22, 2011 6:16 PM
  • Makes sense, when you explain it like that.  Guess I'll stay with what is working :)

    Thanks for helping an ADO newbie along :)

    John

    Saturday, January 22, 2011 6:36 PM
  • You're welcome, John ... glad I could help somewhat. =0)
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, January 22, 2011 7:00 PM