如何从c#.net中的组合框中选择项目时触发事件

时间:2014-01-16 09:04:07

标签: c# winforms

        string Sql_type = "select property_type_id,type_name from lk_tb_property_type";

        OleDbCommand cmd_type = new OleDbCommand(Sql_type, con);

        OleDbDataReader DR_two = cmd_type.ExecuteReader();
        DataTable table_two = new DataTable();
        table_two.Load(DR_two);

        //begin adding line
        DataRow row_two = table_two.NewRow();
        row_two["type_name"] = "Select Poperty Name";
        row_two["property_type_id"] = 0;
        table_two.Rows.InsertAt(row_two, 0);
        //end adding a line


        combo_type.DataSource = table_two;


        combo_type.DisplayMember = "type_name";
        combo_type.ValueMember = "property_type_id";
        combo_type.Text = "Select Poperty Name";

使用此代码我从database.now获取组合框的值。假设我的combobx有两个名为A和B的项目..我还有一个组合框...现在我想要的是当用户从中选择项目A时组合框第二个组合框应该在用户选择项目B时显示与项目A相关的数据,那么应该显示与项目B相关的数据...以便实现这一点...... ??

4 个答案:

答案 0 :(得分:2)

你可以在{obobox1的SelectedIndexChanged事件中获取数据并将其绑定到combobox2

private void combobox1_SelectedIndexChanged(object sender, EventArgs e)
{
     var val = combobox1.SelectedValue;
     // fetch data from database 
     // you need to set SQL parameter value form SelectedValue

     combobox2.DataSource = ...; // set this value 
     combobox2.DisplayMember = .....; // set this value 
     combobox2.ValueMember = ....; // set this value 

}

答案 1 :(得分:0)

请指定一个活动SelectedIndexChangedAutoPostBack = true这是一个C#中的网络应用程序

答案 2 :(得分:0)

在组合框的 SelectedIndexChanged 中编写代码。并使你的组合框的 AutoPostBack = true

答案 3 :(得分:0)

您可以执行以下步骤。 首先,将数据绑定到comboBox1(我假设您的第一个ComboBox名为“comboBox1”,而您的表单名为“Form1”),请确保您的SQL查询命令对于comboBox1是正确的

private void Form1_Load(object sender, EventArgs e)
{
    OleDbConnection con = new OleDbConnection(constr);

    con.Open();
    string Sql_cust_name = "select customer_name from tb_customer";

    OleDbCommand cmd_cust_name = new OleDbCommand(Sql_cust_name, con);

    OleDbDataReader DR_cust_name = cmd_cust_name.ExecuteReader();
    DataTable table_cust_name = new DataTable();
    table_cust_name.Load(DR_cust_name);

    DataRow row_cust_name = table_cust_name.NewRow();
    row_cust_name["customer_name"] = "Select Customer Name";

    table_cust_name.Rows.InsertAt(row_cust_name, 0);

    combo_cust_name.DataSource = table_cust_name;
    combo_cust_name.DisplayMember = "customer_name";
    combo_cust_name.ValueMember = "customer_name";
    combo_cust_name.Text = "Select Customer Name";

    con.Close();
}

接下来,将数据绑定到comboBox2(我假设你的第二个ComboBox名为“comboBox2”),每当它被改变时你必须得到comboBox1.SelectedValue,这个值将用于过滤comboBox2中的数据,所以你必须为comboBox1处理SelectedIndexChanged事件,请确保在项目的某个地方有这个代码:this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

要将数据绑定到comboBox2(我想你的第二个ComboBox名为“comboBox2”),只要它被更改就必须得到comboBox1.SelectedValue

private void combo_cust_name_SelectedIndexChanged(object sender, EventArgs e)
{
    OleDbConnection con = new OleDbConnection(constr);

    con.Open();

    string customerName = "";
    if (combo_cust_name.SelectedValue.GetType() == typeof(DataRowView))
    {
        DataRowView selectedRow = (DataRowView)combo_cust_name.SelectedValue;
        customerName = selectedRow["customer_name"].ToString();
    }
    else
    {
        customerName = combo_cust_name.SelectedValue.ToString();
    }

    string Sql2 = "SELECT customer_number FROM tb_customer WHERE customer_name = '" + customerName + "'";
    OleDbCommand cmd_type = new OleDbCommand(Sql2, con);

    OleDbDataReader DR_two = cmd_type.ExecuteReader();
    DataTable table_two = new DataTable();
    table_two.Load(DR_two);

    DataRow row_two = table_two.NewRow();
    row_two["customer_number"] = "Select Customer Number";
    table_two.Rows.InsertAt(row_two, 0); 

    comboBox2.DataSource = table_two;
    comboBox2.DisplayMember = "customer_number";
    comboBox2.ValueMember = "customer_number";
    comboBox2.Text = "Select Customer Number"; 
}

请根据需要更正SQL查询命令,但不要忘记像上面的示例代码一样放置正确的过滤器。