简体   繁体   English

组合框“所选文本已更改”事件未触发

[英]combobox Selected Text changed event not firing

This is my code. 这是我的代码。 After adding data through DataSource , The SelectedIndexChanged event is not firing. 通过DataSource添加数据后, SelectedIndexChanged事件没有触发。

try
{
    comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

    //comboBox1.Items.Clear();

    comboBox1.ResetText();

    Cn.Open();
    SqlCommand Cd = new SqlCommand("Select Distinct Mobile From Client_Details Where Branch = '" + label2.Text + "'", Cn);
    SqlDataReader da = Cd.ExecuteReader();
    DataTable dt = new DataTable();

    dt.Columns.Add("Mobile", typeof(string));
    dt.Load(da);
    comboBox1.DisplayMember = "Mobile";
    comboBox1.DataSource = dt;
    comboBox1.SelectedItem = "<Select>";
}
catch
{

}
finally
{
    Cn.Close();
    Clear();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    //Label CBSL = new Label();
    //string CBSL = this.comboBox1.SelectedItem.ToString();

    if (comboBox9.Text == "Client")
    {
        Update_Read_Client();
    }
    else if (comboBox9.Text == "Customer")
    {
        Update_Read();
    }
}
  1. It selects the first value again and again. 它一次又一次地选择第一个值。
  2. I had tried DropDownStye = DropDownList., But it become dormant. 我试过了DropDownStye = DropDownList。,但是它变得休眠了。 No values is added. 没有添加任何值。
  3. Any help to resolve my problem. 任何解决我的问题的帮助。

I'm not sure if you have "< Select >" item saved in your database. 我不确定数据库中是否保存了“ <Select>”项。 Also, when using DataTable it's easier to fill it using SqlDataAdapter . 另外,使用DataTable时,使用SqlDataAdapter填充它更容易。 You should also use parameters instead of string concat when you're writing your query like this and using keyword closes your connection automatically when you're done using it. 像这样编写查询时,还应该使用参数而不是字符串concat, using完关键字后, using keyword会自动关闭连接。 I'd write it like this probably: 我可能会这样写:

comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

using (SqlConnection con = new SqlConnection(connectionString))
{
    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = @"SELECT DISTINCT Mobile
                        FROM Client_Details
                        WHERE Branch = @Branch";
    cmd.Parameters.AddWithValue("@Branch", label2.Text);

    try
    {
        var dt = new DataTable();
        dt.Columns.Add("Mobile", typeof(string));

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);

        DataRow row = dt.NewRow();
        row["Mobile"] = "<Select>";                    
        dt.Rows.InsertAt(row, 0);

        comboBox1.DisplayMember = "Mobile";
        comboBox1.DataSource = dt;
        comboBox1.SelectedItem = "<Select>";
    }
    catch
    {
        throw;
    }
}

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

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