Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用SQL数据表和与该表分离的项填充一个组合框_C#_Sql_Combobox - Fatal编程技术网

C# 如何使用SQL数据表和与该表分离的项填充一个组合框

C# 如何使用SQL数据表和与该表分离的项填充一个组合框,c#,sql,combobox,C#,Sql,Combobox,抱歉,如果标题有点模糊-很难用一句话描述我想做什么,所以我会快速解释我想实现什么 我想将一个SQL表绑定到一个组合框,并拥有它的所有值(例如,项名称),但我也希望将我自己的一个项添加到同一个组合框中,与表分开(例如,在组合框中加载SQL表值后,应在列表底部插入“add new”) 这可能吗?如果是,我该怎么做 谢谢大家 您可以使用插入,您必须指定索引和新的项目以添加到列表中 cmb1.Items.Insert(tabl.rows.count,"Add new"); 或 ComboboxItem

抱歉,如果标题有点模糊-很难用一句话描述我想做什么,所以我会快速解释我想实现什么

我想将一个SQL表绑定到一个组合框,并拥有它的所有值(例如,项名称),但我也希望将我自己的一个项添加到同一个组合框中,与表分开(例如,在组合框中加载SQL表值后,应在列表底部插入“add new”)

这可能吗?如果是,我该怎么做


谢谢大家

您可以使用插入,您必须指定索引和新的项目以添加到列表中

cmb1.Items.Insert(tabl.rows.count,"Add new");

ComboboxItem item = new ComboboxItem();
item.Text = "Add new";
item.Value = 0;

cmb1.Items.Add(item);

如果您没有指定它最后将附加的任何索引

您可以使用插入,您必须指定索引和要添加到列表中的新

cmb1.Items.Insert(tabl.rows.count,"Add new");

ComboboxItem item = new ComboboxItem();
item.Text = "Add new";
item.Value = 0;

cmb1.Items.Add(item);

如果您没有指定它最后将附加的任何索引,您可以像这样在代码隐藏中分配数据源

cmb1.DataSource=dt;
cmb1.DataBind();
cmb1.Items.Insert(dt.Rows.Count,new ComboBoxItem("Add New","0"));
cmb1.appenddatabounditems="true";

您可以像这样在代码隐藏中分配数据源

cmb1.DataSource=dt;
cmb1.DataBind();
cmb1.Items.Insert(dt.Rows.Count,new ComboBoxItem("Add New","0"));
cmb1.appenddatabounditems="true";

你是说组合框。项目。添加(你的项目)?嗯,是的。最初-将数据集(例如:客户端)插入combobox.yup后,在数据集绑定后执行相同的操作您的意思是combobox.Items.Add(您的项)?嗯,是的。最初-在将数据集(例如:Clients)插入combobox.yup后,在数据集绑定后执行相同的操作好的,我理解。有没有办法将其添加到列表的底部,也就是说将其添加到最后一个索引的位置?我已经为您提供了最后一个索引的代码。tabl.rows.count-1=ComboBox中的项目总数好的,我理解。有没有办法将其添加到列表的底部,也就是说将其添加到最后一个索引的位置?我已经为您提供了最后一个索引的代码。tabl.rows.count-1=组合框中的项目总数