简体   繁体   English

如何修复“组合框选择的索引始终显示第一个值C#”

[英]How to fix 'combobox selected index always displaying the first value C#'

I have a windows form app that on load fills up my combobox with the data from mysql. 我有一个Windows窗体应用程序,在加载时会用来自mysql的数据填充我的组合框。 But on load I need to hava an empty value first and not selecting the very first from the combobox. 但是在加载时,我需要首先获取一个空值,而不要从组合框中选择第一个。

I have tried combobox.SelectedIndex = -1 ; 我已经尝试了combobox.SelectedIndex = -1 ; It works but I used a message prompt to debug and I can see that the message displays the first value from the combobox before displaying none. 它可以工作,但是我使用消息提示进行调试,并且可以看到消息在显示任何内容之前都显示了组合框中的第一个值。

void Fillcombo()
{
    DataTable tb = new DataTable("candidate_l");
    connection.Open();
    string QueryPres = "SELECT candidate_nu, CONCAT(candidate_n, ' ' ,candidate_s) AS fullname FROM candidate_l WHERE candidate_p = 'PRES'";
    cmd = new MySqlCommand(QueryPres, connection);
    mdr = cmd.ExecuteReader();

    tb.Load(mdr);

    cbo_President.ValueMember = "candidate_nu";
    cbo_President.DisplayMember = "fullname";
    cbo_President.DataSource = tb;
    cbo_President.SelectedIndex = -1;
    cbo_President.Text = "No Selection";

    connection.Close();
}

private void cbo_President_SelectedIndexChanged(object sender, EventArgs e)
{
    if (cbo_President.SelectedIndex > -1)
    {
        string cbopres = cbo_President.SelectedValue.ToString();
        MessageBox.Show("Candidate ID : " + cbopres);
    }
    else if (cbo_President.SelectedIndex == -1)
    {
        MessageBox.Show("Candidate ID : none");
    }
}

I need to get the message Candidate ID : none on the else if statement. 我需要获取消息Candidate ID : none在else if语句上Candidate ID : none Because I have been getting the Candidate ID for the first item on the combobox. 因为我一直在获取组合框上第一项的Candidate ID

A common pattern to solve this it to remove the event handler when you are setting up/initializing your controls and add the event handler after initialization. 解决此问题的一种常见模式是在设置/初始化控件时删除事件处理程序,并在初始化后添加事件处理程序。

        comboBox1.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
        comboBox1.DataSource = test;
        comboBox1.SelectedIndex = -1;
        comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;

after setting the datasource for your cbo_President object, insert a blank option at position 0 and just do SelectedIndex = 0 . 在为cbo_President对象设置数据源之后,在位置0插入空白选项,然后执行SelectedIndex = 0 ie. 即。

cbo_President.ValueMember = "candidate_nu";
cbo_President.DisplayMember = "fullname"; 
cbo_President.DataSource = tb;
cbo_President.Items.Insert(0, "No Selection");
cbo_President.SelectedIndex = 0;

You can add an "empty" row to your DataTable before binding it to combobox: 您可以在将DataTable绑定到组合框之前向其DataTable添加“空”行:

DataRow dr = tb.NewRow();
dr["candidate_nu"] = -1;
dr["fullname"] = string.Empty; // or "No Selection"

tb.Rows.InsertAt(dr, 0);

cbo_President.ValueMember = "candidate_nu";
cbo_President.DisplayMember = "fullname";
cbo_President.DataSource = tb;

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

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