简体   繁体   English

如何设置默认组合框

[英]How to Set default combobox

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. 先感谢您。

cbxCategory.SelectedIndex should be set to an integer from 0 to Items.Count-1 like cbxCategory.SelectedIndex应设置为0Items.Count-1的整数

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],但类似的东西应该工作

It looks like you're searching the cbxCategory.Items collection for a string, but it contains items of type ComboBoxItem. 看起来您正在搜索cbxCategory.Items集合中的字符串,但它包含ComboBoxItem类型的项目。 Therefore the search will return -1. 因此搜索将返回-1。

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;
    }
}

Here's my simple solution 这是我的简单解决方案

        var list = comboBox1.Items.Cast<string>().ToList();
        cbxCategory.SelectedIndex = list.FindIndex(c => c.StartsWith("test"));

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;

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

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