从 C# 中的数据集绑定后在组合框中插入项目 [英] insert item in combobox after binding it from a Dataset in c#
本文介绍了从 C# 中的数据集绑定后在组合框中插入项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在从数据集绑定组合框后,我必须在顶部插入选择".我尝试了 this 但它不起作用.抛出错误数据集没有任何演员定义".我想我没有正确使用它.注释代码是我尝试过但不起作用的部分.
I have to insert "Select" at top after combobox is bound from dataset.i tried this but 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>());
推荐答案
您必须插入数据绑定到的对象而不是组合框.您不能直接插入组合框.
You have to Insert to the object you are databinding to rather than to the combobox. You can't insert directly into the combobox.
你可以使用这个:
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.
这篇关于从 C# 中的数据集绑定后在组合框中插入项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文