【发布时间】:2012-10-27 05:00:06
【问题描述】:
您好,我是 C# 新手。我尝试了以下方式将新项目添加到我绑定的 ComboBox 中,但它不会给出任何结果。
是否可以向绑定的 ComboBox 添加新项目(这里的问题是 ID 是 Bigint 数据类型,但我想添加 Select ID)?
如果可以,请提供一段代码
try
{
objSqlExecute.OpenConnection();
string strQuery = objQueryManager.GetEmployeeRecords();
//Add Extra Items to combo Box
cmbEmployeeID.Items.Add("Select Id");
DataTable dtEmployee = objSqlExecute.GetRecordExecution(strQuery);
// DataRow dtNew = dtEmployee.NewRow();
// dtNew["ID"] = "Select ID";
// dtNew["FName"] = "";
//dtEmployee.Rows.InsertAt(dtNew, 0);
cmbEmployeeID.DataSource = dtEmployee;
cmbEmployeeID.DisplayMember = "ID";
cmbEmployeeID.ValueMember = "FName";
}
【问题讨论】:
-
设置数据源后尝试添加项
-
它显示
Items collection cannot be modified when the DataSource property is set这个异常@Amiram Korach -
将它添加到表格时发生了什么?
-
如果我删除对这些行的评论
DataRow dtNew = dtEmployee.NewRow(); dtNew["ID"] = "Select ID"; dtNew["FName"] = ""; dtEmployee.Rows.InsertAt(dtNew, 0);它给出了类似Input string was not in a correct format.Couldn't store <Select ID> in ID Column. Expected type is Byte的错误。我知道,因为此列是bigint数据类型 -
为什么要显示 ID?更常见的是显示名称并使用 ID 作为值。
标签: winforms c#-4.0 combobox datasource