C# 如何设置默认组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14589964/
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
How to Set default combobox
提问by Roel
So I've been looking to set a default value for my combobox. I found a few things but none of them seem to work.
所以我一直在寻找为我的组合框设置默认值。我发现了一些东西,但它们似乎都不起作用。
Actually, it works if I create a simple combobox and use comboBox1.SelectedIndex = comboBox1.Items.IndexOf("something")
but once I dynamically generate the contents of the comboboxes, I can't get it to work anymore.
实际上,如果我创建一个简单的组合框并使用comboBox1.SelectedIndex = comboBox1.Items.IndexOf("something")
它就可以工作,但是一旦我动态生成组合框的内容,我就无法再使用它了。
This is how I fill my combo box (located in the class's constructor);
这就是我填充组合框(位于类的构造函数中)的方式;
string command = "SELECT category_id, name FROM CATEGORY ORDER BY name";
List<string[]> list = database.Select(command, false);
cbxCategory.Items.Clear();
foreach (string[] result in list)
{
cbxCategory.Items.Add(new ComboBoxItem(result[1], result[0]));
}
I can't seem to get it to work to set a default value, like if I place cbxCategory.SelectedIndex = cbxCategory.Items.IndexOf("New")
below the above code, it won't work.
我似乎无法让它设置默认值,就像我放在cbxCategory.SelectedIndex = cbxCategory.Items.IndexOf("New")
上面的代码下面一样,它不会工作。
WinForms, by the way.
顺便说一下,WinForms。
Thank you in advance.
先感谢您。
采纳答案by iltzortz
cbxCategory.SelectedIndex
should be set to an integer from 0
to Items.Count-1
like
cbxCategory.SelectedIndex
应该设置为一个整数 from 0
to Items.Count-1
like
cbxCategory.SelectedIndex = 2;
your
您的
cbxCategory.SelectedIndex = cbxCategory.Items.IndexOf("New")
should return -1 as long as no ComboboxItem mutches the string ("New");
只要没有 ComboboxItem 替换字符串(“New”),就应该返回 -1;
another solution though i don't like it much would be
另一种解决方案虽然我不太喜欢它
foreach(object obj in cbxCategory.Items){
String[2] objArray = (String[])obj ;
if(objArray[1] == "New"){
cbxCategory.SelectedItem = obj;
break;
}
}
perhaps this also requires the following transformation to your code
也许这还需要对您的代码进行以下转换
foreach (string[] result in list)
{
cbxCategory.Items.Add(result);
}
I haven't tested the code and i am not sure about the casting to String[2] but something similar should work
我还没有测试过代码,我不确定是否转换为 String[2] 但类似的东西应该可以工作
回答by RogerN
It looks like you're searching the cbxCategory.Items collection for a string, but it contains items of type ComboBoxItem. Therefore the search will return -1.
看起来您正在 cbxCategory.Items 集合中搜索字符串,但它包含 ComboBoxItem 类型的项目。因此搜索将返回-1。
回答by kmatyaszek
You can use LINQ.
您可以使用 LINQ。
//string command = "SELECT category_id, name FROM CATEGORY ORDER BY name";
//List<string[]> list = database.Select(command, false);
// sample data...
List<string[]> list = new List<string[]> { new string[] { "aaa", "bbb" }, new string[] { "ccc", "ddd" } };
cbxCategory.Items.Clear();
foreach (string[] result in list)
{
cbxCategory.Items.Add(new ComboBoxItem(result[1], result[0]));
}
ComboBoxItem tmp = cbxCategory.Items.OfType<ComboBoxItem>().Where(x => x.ResultFirst == "bbb").FirstOrDefault();
if (tmp != null)
cbxCategory.SelectedIndex = cbxCategory.Items.IndexOf(tmp);
ComboBoxItem
class:
ComboBoxItem
班级:
class ComboBoxItem
{
public string ResultFirst { get; set; }
public string ResultSecond { get; set; }
public ComboBoxItem(string first, string second)
{
ResultFirst = first;
ResultSecond = second;
}
}
回答by spajce
Here's my simple solution
这是我的简单解决方案
var list = comboBox1.Items.Cast<string>().ToList();
cbxCategory.SelectedIndex = list.FindIndex(c => c.StartsWith("test"));
回答by Nikolay Zhukov
My solution:
我的解决方案:
int? defaultID = null;
foreach (DataRow dr in dataSource.Tables["DataTableName"].Rows)
{
if ((dr["Name"] != DBNull.Value) && ((string)dr["Name"] == "Default Name"))
{
defaultID = (int)dr["ID"];
}
}
if (defaultID != null) comboBox.SelectedValue = defaultID;