开发者

When are win form controls data bound?

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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜