3 回答
TA贡献1951条经验 获得超3个赞
更新
在包含组合框的主窗体中。我猜你的代码是这样的
private void btnAddNewObjectsButton_Click(object sender, EventArgs e)
{
AddNewObjectsForm form2 = new AddNewObjectsForm();
form2.ShowDialog();
if (form2.isSuccess)
{
this.myComboBox.DataSource = null;
this.myComboBox.Items.Clear();
this.myComboBox.DataSource = db.Object.ToList();//If you work with Entity frame work
cmbCustomer.ValueMember = "Id";
cmbCustomer.DisplayMember = "Name";
}
}
在另一种形式上,您的代码将是这样的
public partial class AddNewdbObjects : Form
{
//isSuccess is a flage that will be true if the new object is added to db or no
public isSuccess = false;
//After Constructor in your click event
private void btnSave_Click(object sender, EventArgs e)
{
//Intialize data base source;
_db = new DBEntities();
dbObject obj = new dbObject();
obj.Name = txtName.Text;
try
{
_db.dbObject.Add(cust);
_db.SaveChanges();
isSuccess = true;
this.Close();
}
catch (Exception exc)
{
isSuccess = false;
}
}
}
此解决方案应该适用于您。
TA贡献1828条经验 获得超6个赞
尝试这个:
DataTable table = new DataTable();
DataRow row;
DataColumn column;
// Create new DataColumn, set DataType, ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "ValueMember";
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = Type.GetType("System.String");
column.ColumnName = "DisplayMember";
table.Columns.Add(column);
row = table.NewRow();
row["ValueMember"] = 1;
row["DisplayMember"] = "item";
table.Rows.Add(row);
comboBox1.DataSource = null;
comboBox1.DataSource = table;
comboBox1.DisplayMember = "DisplayMember";
comboBox1.ValueMember = "ValueMember";
我希望这可以帮助你 :)
TA贡献2080条经验 获得超4个赞
我所要做的就是填写 TableAdapter 然后刷新组合框:
tblTableAdapter.Fill(dsMy.tbl); myComboBox.Refresh();
- 3 回答
- 0 关注
- 536 浏览
添加回答
举报