在组合框中选择不同项目时如何动态更改组合框

在组合框中选择不同项目时如何动态更改组合框

问题描述:

您好,专家,

我有2个组合框.

如果用户在加载页面上的combobox1中选择了1个项目,则该值的相关项目将显示在combobox 2中.

我已经在组合框1中获取了值,这是代码...

Hi experts,

i have 2 combobox.

if user select 1 item in combobox1 on load page ,then related items of that value will displayed in combobox 2.

i have fetch the value in combobox 1 here are the code......

HERE ARE THE TABLE1 :
PROCESS                  ID     MATERIAL
Wraping	                 1	AFA
Moulding	         2	AFA
WiperIDFinishing	 3	AFA





TABLE 2:
MATERIAL
AFA









private void InventoryEntry_Load(object sender, EventArgs e)
       {

 try
            {
                SqlDataAdapter da = new SqlDataAdapter("select Material  from Master.Materialname", objConn1);
                DataTable dt = new DataTable();
                da.Fill(dt);
                DataRow dr;
                dr = dt.NewRow();
                //dr.ItemArray = new object[] { 0, "---Select an item---" };
                //dt.Rows.InsertAt(dr, 0);
                comboBox3.DisplayMember = "Material";
                comboBox3.ValueMember = "Material";
                comboBox3.DataSource = dt;
                objConn1.Close();
            }
            catch (Exception ex)
            {
            }

}

只需使用参数
编写sql
Just write sql with parameter
select Material  from Child.Materialname
where Child.Master = @master



使用此sql在第一个组合的更改事件上重新加载第二个组合框.由于您没有编写女巫组合框,因此我无法确定该事件的命名方式,我猜是ValueChanged



Use this sql to reload second combobox on change event of first combo. Since you did not write witch combobox you are using I can''t tell how this event is named I would guess ValueChanged

protected void combo1_ValueChanged(object source , ValueChangedArgs args)
{
 FetchForCombo2WithMasterParam(combo1.Value);
}



然后在FetchForCombo2WithMasterParam中,使用来自combo1.Value的参数执行上面的sql.



And in FetchForCombo2WithMasterParam you would execute sql above with param from combo1.Value.


私有无效comboBox3_SelectedIndexChanged(object sender,EventArgs e)
{
字符串sql2 =从Master.ProductionProcess中选择进程作为内部联接,将Master.Materialname作为b在a.Material = b.mid WHERE b.material =""+ comboBox3.SelectedValue.ToString()+"'';
SqlDataAdapter da =新的SqlDataAdapter(sql2,objConn1);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr;
dr = dt.NewRow();
comboBox4.DisplayMember =进程";
comboBox4.ValueMember =进程";
comboBox4.DataSource = dt;
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
string sql2= "select process from Master.ProductionProcess as a inner join Master.Materialname as b on a.Material = b.mid WHERE b.material =''" + comboBox3.SelectedValue.ToString()+ "''";
SqlDataAdapter da = new SqlDataAdapter(sql2,objConn1);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr;
dr = dt.NewRow();
comboBox4.DisplayMember = "process";
comboBox4.ValueMember = "process";
comboBox4.DataSource = dt;
}