简体   繁体   English

如何在C#中的Winform应用程序中设置combobox第一个元素

[英]How to set combobox first element in winform application in c#

I am new in Winform application, here i am using Entity framework, when i binding values to a combobox from sql table, i need to set first combobox item as "Please select", how can i set this..? 我是Winform应用程序的新手,在这里我使用的是Entity Framework,当我将值从sql表绑定到组合框时,我需要将第一个组合框项目设置为“请选择”,我该如何设置呢?

var qry = context.Tbl_EmployeeDetails.Where(x => x.IsDeleted == false).ToList();          
            if(qry!=null)
            {                
                drpname.ValueMember = "RecordId";
                drpname.DisplayMember = "Name";              
                drpname.DataSource = qry;                          
            }

how can i set this first item as "please select" and value '0' 我如何将第一项设置为“请选择”并将值设置为“ 0”

another suggestion in winform that.. how can i set value in a datagridview linkbutton column when i set 'Edit', 'Delete' columns are as linkbutton Winform中的另一个建议是..当我将“编辑”,“删除”列设置为链接按钮时,如何在datagridview链接按钮列中设置值

You can insert that item in the beginning of the list. 您可以将该项目插入列表的开头。

Code: 码:

var items = db.Tbl_EmployeeDetails.Where(x => x.IsDeleted == false).ToList();
items.Insert(0,new Tbl_EmployeeDetail() { RecordId= 0, Name = "[Please Select an Item]" });

drpname.DropDownStyle = ComboBoxStyle.DropDownList; //optional
drpname.ValueMember = "RecordId";
drpname.DisplayMember = "Name";
drpname.DataSource = items;
drpname.SelectedIndex = 0;

Screenshot: 截图:

在此处输入图片说明

Note 注意

To have a hint in ComboBox without adding an item, take a look at following post: 要在ComboBox提示而不添加项目,请查看以下文章:

ComboBox选择文本而不添加项目ComboBox选择文本而不添加项目

After you load your var qry which will be a List<string> you want to do the following 加载将为List<string> var qry ,您需要执行以下操作

var qry = context.Tbl_EmployeeDetails.Where(x => x.IsDeleted == false).ToList();   

if(qry!=null)
{                
    drpname.ValueMember = "RecordId";
    drpname.DisplayMember = "Name";              
    drpname.DataSource = qry;  
    drpname.Items.Insert(0, "--Please Select--");   
    drpname.SelectedIndex = 0;                     
}

Or you could have easily added it to qry since you are returning the data ToList() 或者您可以轻松地将其添加到qry,因为您要返回数据ToList()

for example 例如

var qry = context.Tbl_EmployeeDetails.Where(x => x.IsDeleted == false).ToList();   
qry.Insert(0, "--Please Select--");

if(qry!=null)
{                
    drpname.ValueMember = "RecordId";
    drpname.DisplayMember = "Name";              
    drpname.DataSource = qry; 
    drpname.SelectedIndex = 0; 
}

Your combobox cannot contain additional items if you are binding it to a data source. 如果将组合框绑定到数据源,则组合框不能包含其他项目。 You have a few options here: 您可以在此处选择以下几种方式:

  1. Don't bind it to a data source, ie populate it manually from your data set by looping through the items in your query and adding them individually to the combobox; 不要将其绑定到数据源,即通过遍历查询中的项目并将它们分别添加到组合框中,从数据集中手动填充它; this will allow you the opportunity to first add your "Please Select" item. 这将使您有机会首先添加“请选择”项。 This can be complicated because you will actually have to add the string names as the items in the combobox, and then you can set the item's Tag property to the actual query row to reference it later. 这可能很复杂,因为实际上您必须将字符串名称添加为组合框中的项目,然后可以将项目的Tag属性设置为实际的查询行,以便以后引用它。

  2. Include a fake item returned from your query that has a display member of "please select". 包括从查询中返回的显示项目为“请选择”的假项目。 Note, I don't recommend this approach because you're allowing your presentation logic to leak into your data/business layer. 注意,我不推荐这种方法,因为您正在允许表示逻辑泄漏到数据/业务层。

  3. Use a third-party control instead of the win forms combobox. 使用第三方控件而不是Win窗体组合框。 I've used Infragistics and they have the ability to display a "suggestion" that's not actually in the list when nothing is selected. 我使用过Infragistics,当没有选择任何内容时,它们可以显示不在列表中的“建议”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM