1

I have a bunch of controls in a tab control on a windows form. Some of the controls are data bound. I'm attempting to access the values of the controls but some of the controls seem to not have values until i physically navigate to the form that has the control. When are controls data bound? Do they have to be displayed first before the values are actually set?

2 Answers 2

1

I had accurately same problem,whenever i wanted to read combobox default value from unnanvigated tabpages was returning null,and i founddatabanding occures aftercontrol show, and what i did ,was writing this function

protected virtual void SetComboData(System.Windows.Forms.Control parentCtrl, DataRow r)
{
    foreach (System.Windows.Forms.Control ctrl in parentCtrl.Controls)
    {
        if (ctrl is ComboBox)
            if ((ctrl as ComboBox).DataBindings.Count != 0)
                (ctrl as ComboBox).SelectedValue = r[(ctrl as ComboBox).DataBindings[0].BindingMemberInfo.BindingMember];
        if (ctrl is TextBox)
            if ((ctrl as TextBox).DataBindings.Count != 0)
                (ctrl as TextBox).Text = r[(ctrl as TextBox).DataBindings[0].BindingMemberInfo.BindingMember].ToString();
        SetLecData(ctrl, r);
    }
}

and i was calling the function before saving dataset

 SetComboData(tabControl1, MyDataSet.Table["MyTable"]);//for tabcontrol
1
  • Is there any way around having to have the control show before the binding occurs?
    – MBU
    May 31, 2011 at 19:33
0

This is because the tab control isn't 'initialized' until it's selected. The workaround for this is to subclass the tabcontrol and 'intialize' the tab pages whenever they're added. See Does data binding work on invisible control? for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.