重新绑定时,组合框项目消失 [英] ComboBox Items Disappearing while Re binding
问题描述
在我的应用程序中,我有一个组合框,其中包含一些来自DB的数据.组合框数据源已设置为绑定源.绑定源的数据集为数据源,数据表为数据成员.
添加,编辑或删除条目后,我将更新此组合框.通过再次调用绑定代码来完成更新.现在,组合框中的所有项目都消失了.
我不知道怎么回事.
链接 http://www.filedropper.com/students_1 中的示例作品.
In my application i have a combo-box with list of some data from DB. The combo-box data-source is given set to a binding source. The binding source has a data-set as Data-source and a Data-table as data-member.
I update this combo-box after adding or editing or deleting an entry. The updating is done by again calling the code for binding. Now all the items in the combo-box disappears.
I don''t know how it happens.
A sample work is in the link http://www.filedropper.com/students_1. the db is included with it.
推荐答案
更改定义:
Change the definitions:
DBOprX dbOpr = new DBOprX();
BindingSource bs = new BindingSource(new System.ComponentModel.Container());
DataSet ds = new DataSet();
至:
to:
DBOprX dbOpr;
BindingSource bs;
DataSet ds;
更改GetData()
Change the GetData()
void GetData()
{
dbOpr = new DBOprX();
bs = new BindingSource(new System.ComponentModel.Container());
ds = new DataSet();
ds = dbOpr.SelectMSSQLQry("Select * FROM Stu", "Students");
bs.DataMember = "Students";
bs.DataSource = ds;
cboStudents.DataSource = bs;
cboStudents.DisplayMember = "Name";
cboStudents.ValueMember = "Id";
cboStudents.SelectedIndex = -1;
}
这篇关于重新绑定时,组合框项目消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!