如何在win形式的梳子盒中添加额外的项目 [英] how to add extra item in comb box in win form
本文介绍了如何在win形式的梳子盒中添加额外的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在数据源的组合框中添加额外的项目?
我的代码是
I want to know that how to add extra item in combo box with datasource?
My code is
public Fill(List<string> contractor)
{
comboBox1.Sorted = true;
contractor.Insert(0, "All");
comboBox1.DataSource = contractor;
}
添加数据源后,项目( 全部)显示在两者之间。
但是我想在组合框上显示它。
After adding datasource, item("All") is shown in between.
But I want to show it on top of combo box.
推荐答案
你好,
如果您使用DataTable与combobox绑定,请尝试此代码
Hello ,
if you are using DataTable to bind with combobox then try this code
DataRow dr = dt.NewRow(); //dt is DataTable
dr[0] = "Please Select";//Add the item in Datarow.Here at 0 position. you can change the position according to your requirement
dt.Rows.InsertAt(dr, 0);//add the row in datatable at top position
cmbcategory.DataSource = dt;
cmbcategory.DisplayMember = "Category";
cmbcategory.ValueMember = "Id";
谢谢
thanks
排序组合框总是按字母顺序对列表进行排序。因此,将排序设置为false,然后将其插入完美。
如果要对列表进行排序,请在插入前调用List.Sort,然后分配给数据来源。
希望这些帮助
试试
try
Datatable Dt = new Datatable();
Dt.Columns.Add("Id");
Dt.Columns.Add("Name");
Dt.Rows.Add("0","Item 1");
Dt.Rows.Add("1","Item 2");
combobox.DataSource=Dt;
combobox.ValueMember="Id";
combobox.DisplayMember="Name";
这篇关于如何在win形式的梳子盒中添加额外的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文