从 c# 中的数据集绑定后在组合框中插入项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11374880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
insert item in combobox after binding it from a Dataset in c#
提问by vatsal
I have to insert "Select" at top after combobox is bound from dataset.i tried thisbut it isn't working.Throws error "dataset doesn't have any definition for cast".I think i am not using it properly.Commented code is the part i tried but not working.
从数据集绑定组合框后,我必须在顶部插入“选择”。我试过这个,但它不起作用。抛出错误“数据集没有任何演员定义”。我认为我没有正确使用它。评论代码是我尝试过但不起作用的部分。
cmbCategory.DataSource = dsCat.Tables[0];
cmbCategory.DisplayMember = "CategoryName";
cmbCategory.ValueMember = "ID";
// cmbCategory.Items.Add("Select");
// cmbCategory.SelectedText = "Select";
// cmbCategory.DataSource =(new object[] { "Select" }).Concat(this.liveReportingDalc.GetCategoriesByType(CategoryType.RegistrationType).Cast<object>());
采纳答案by Vishal Suthar
You have to Insert to the object you are databinding to rather than to the combobox. You can't insert directly into the combobox.
您必须插入数据绑定到的对象而不是组合框。您不能直接插入组合框。
You can use this :
你可以使用这个:
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("CategoryName");
DataRow dr = dt.NewRow();
dr["CategoryName"] = "Select";
dr["ID"] = 0;
dt.Rows.InsertAt(dr, 0);
cmbCategory.DisplayMember = "CategoryName";
cmbCategory.ValueMember = "ID";
cmbCategory.DataSource = dt;
cmbCategory.SelectedIndex = 0;
This is very straight forward example.
这是一个非常直接的例子。
回答by Asif Mushtaq
You cannot add items to a ComboBoxafter binding it to a data source. To add or remove items from a ComboBoxwith a bound data source, you have to do it through data source itself.
在将 aComboBox绑定到数据源后,您无法向 a 添加项目。要从ComboBox绑定数据源中添加或删除项目,您必须通过数据源本身来完成。
You can insert a DataRowinto your table and it will be automatically added to your ComboBox. Try the following:
您可以将 a 插入DataRow到您的表中,它会自动添加到您的ComboBox. 请尝试以下操作:
DataRow dr = dsCat.Tables[0].NewRow();
dr["CategoryName"] = "Select";
dr["ID"] = 123;// Some ID
dsCat.Tables[0].Rows.Add(dr);
回答by Andyz Smith
// cmbCategory.DataSource =(new object[] { "Select" }).Concat(this.liveReportingDalc.GetCategoriesByType(CategoryType.RegistrationType).Cast<object>());
You might be able to do this, but your syntax is wrong somehow.
您也许能够做到这一点,但您的语法不知何故是错误的。
Maybe you can split it up until you figure it out and then compress it back into in-line functions.
也许您可以将其拆分,直到您弄清楚,然后再将其压缩回内联函数。
List <object> catData = new List <object> { "Select" };
DataSet catByType = this.liveReportingDalc.GetCategoriesByType(CategoryType.RegistrationType);
foreach(DataRow oRow in catByType.Tables[0].Rows)
{ catData.Add(oRow.ItemArray[0]); }
But for this to work you need to consolidate your understanding of the data coming back from the GetCategoriesByTypefunction. Will the objects be text like "Select"?.
但要使其发挥作用,您需要巩固对从GetCategoriesByType函数返回的数据的理解。对象会是像“选择”这样的文本吗?。

