.net 如何向绑定的组合框添加空值或自定义值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1449915/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 13:20:42  来源:igfitidea点击:

How to add empty or custom value to a bound combobox?

.netdata-bindingcombobox

提问by Dan

I am using a System.Windows.Forms.Combobox bound to Category table as search criteria. I need to have a value “All” or empty string that will be selected when user does not want to use this criteria. Since combo is bound, each time it is clicked, value added by combo1.Text = “All” is erased. Obviously, I can't add “All” category to database. What is the best way to accomplish this?

我使用绑定到类别表的 System.Windows.Forms.Combobox 作为搜索条件。当用户不想使用此条件时,我需要有一个值“全部”或空字符串。由于combo是绑定的,每次点击,combo1.Text = “All”添加的值都会被抹掉。显然,我不能将“全部”类别添加到数据库中。实现这一目标的最佳方法是什么?

采纳答案by MartW

Either manually add the All entry to the bound dataset after the other values have been loaded, or unbind the combo and instead iterate through the data to populate it. It's just a search combo, so you don't really need all the benefits of binding.

在加载其他值后手动将 All 条目添加到绑定数据集,或者取消绑定组合,而是遍历数据以填充它。它只是一个搜索组合,因此您并不真正需要绑定的所有好处。

回答by Neeraj

  1. If you do not want to add the item "All" into your data source, you can do the following:
  1. 如果不想将“全部”项添加到数据源中,可以执行以下操作:

Code Snippet

代码片段

comboBox1.Text = "All";

It sets the text displayed in the comboBox to the value assigned but without changing the items in the comboBox and the bound data source.

它将组合框中显示的文本设置为分配的值,但不更改组合框中的项目和绑定的数据源。

  1. You can add "All" to your datasource as well. But you should do it in this way:
  1. 您也可以将“全部”添加到您的数据源。但是你应该这样做:

Code Snippet

代码片段

private void button1_Click(object sender, EventArgs e)

{

DataRow dataRow = dataTable.NewRow();

dataRow["ItemId"] = "All";

dataTable.Rows.InsertAt(dataRow, 0);

comboBox1.SelectedIndex = 0;

}

The easiest way would be to insert a row into your ds.Tables[0] that is

最简单的方法是在 ds.Tables[0] 中插入一行,即

  ds = StockDAL.BindItemId();

  DataRow dataRow = ds.Tables[0].NewRow();

  dataRow["ItemId"] = "All";

  ds.Tables[0].Rows.InsertAt(dataRow, 0);

  comboBox1.DataSource = ds.Tables[0];
  comboBox1.DisplayMember = "ItemId";
  comboBox1.ValueMember = "ItemId";
  comboBox1.selectedIndex=0;

hope this solve ur issue..

希望这能解决你的问题..

回答by Konstantinos

You would have to add another item:

您将不得不添加另一个项目:

ComboBox1.Items.Insert(0,"All");

回答by Asim Sajjad

  1. If you are using sql server then you can add extra value to the select command for the All option
  2. Or you can add All option by using comboBox1.Items.Insert(0,"All") to add new item as zero index. after binding the control
  1. 如果您使用的是 sql server,那么您可以为 All 选项的 select 命令添加额外的值
  2. 或者,您可以通过使用 comboBox1.Items.Insert(0,"All") 添加新项目作为零索引来添加所有选项。绑定控件后

Hope that will help.

希望这会有所帮助。

回答by ProfK

I always 'manually' add an item to the data source before data binding. This avoids having 'artificial' data in your data source's query, so if you use the query for something other than a dropdown, you only get real data.

我总是在数据绑定之前“手动”向数据源添加一个项目。这可以避免在数据源的查询中包含“人工”数据,因此如果您将查询用于下拉列表以外的其他内容,则只能获得真实数据。

回答by Jidheesh Rajan

you can try this

你可以试试这个

ComboBox1.Items.Insert(0,"All");

while inserting to table you just check the selected index of the ComboBox1 ie,

在插入表时,您只需检查 ComboBox1 的选定索引,即,

if(ComboBox1.SelectedIndex>0)
{
//do the insert code here
}
else
{
//dont do anything
}

this will work fine...

这会正常工作...