简体   繁体   English

根据Combobox1所选项目填充ComboBox2

[英]Populate ComboBox2 depending on Combobox1 selected item

i am a student, and a newbie to programming, i have two comboboxes, combobox1 and combobox2 combobox1 contains mobile company's like nokia,samsung,htc and combobox2 contains mobile models like samsung,s3 and etc, i want to sort the two combobox i mean when i click the nokia in the combobox1 then all the model of nokia should be visible in the list of combobo2, so i have decided to used foreign key relationship 我是一名学生,是编程的新手,我有两个组合框,combobox1和combobox2包含移动公司(如nokia,samsung,htc),combobox2包含移动模型(如samsung,s3等),我想对这两个组合框进行排序当我单击combobox1中的nokia时,nokia的所有模型都应该在combobo2列表中可见,因此我决定使用外键关系

          Manufacturer -table
        - manufacturerid (primary key)
        - name

         Model -table
        - modelid (primary key)
        - manufacturerid (foreign key to manufacturer)
         - name

Example for the data: 数据示例:

Manufacturer table 制造商表

         manufacturerid name
     -------------- ----------
    1              Nokia
    2              Samsung
    3              HTC

Model table 型号表

       modelid manufacturerid name
     ------- -------------- ----------
     1       1              C7
     2       1              Lumia 900
     3       1              Lumia 920
     4       2              Galaxy S II
     5       2              Galaxy S III
     6       3              Desire X
     7       3              Windows Phone 8X
     8       3              One S

i want that if i select nokia in the first combobox then the second combobox will select all the models which are manufactureid = 1 what to use? 我希望如果我在第一个组合框中选择nokia,那么第二个组合框将选择所有制造模型= 1的模型,使用什么? how can i do that? 我怎样才能做到这一点? priviously i was using 我私下里正在使用

 private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
  {
     comboBox3.Text = "";
   if ("samsung" == comboBox4.SelectedItem.ToString())
      {
           comboBox3.DataSource = table1BindingSource;
           comboBox3.ValueMember = "samsung";
           comboBox3.DisplayMember = "samsung";
      }
   if ("htc" == comboBox4.SelectedItem.ToString())
      {
         comboBox3.DataSource = table1BindingSource;
         comboBox3.ValueMember = "htc";
          comboBox3.DisplayMember = "htc";
       }
  }

but i have to update the samsung string each time i add a new model, so i decided to work with tables so that i can update that 但是我每次添加新模型时都必须更新三星字符串,所以我决定使用表,以便可以更新该表

Include a function to retrieve data from database as follows 包括一个从数据库检索数据的功能,如下所示

public DataTable Select(String sqlQuery)
   {       
       con.Open();
       SqlDataAdapter adapter = new SqlDataAdapter(sqlQuery,con);
       DataTable table = new DataTable();
       adapter.Fill(table);
       con.Close();
       return table;
   }

and in Page_Load event 并在Page_Load事件中

protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack)
     {
      String sqlQuery="select manufacturerid,name From Manufacturertable";

      comboBox4.DataSource = cls.Select(sqlQuery);
      comboBox4.DataTextField = "name";
      comboBox4.DataValueField = "manufacturerid";
      comboBox4.DataBind();
   }
 }

and in SelectedIndexChanged event of comboBox4 并在comboBox4 SelectedIndexChanged事件中

protected void comboBox4_SelectedIndexChanged(object sender, EventArgs e) {
    String sqlQuery="select modelid,name From Modeltable where manufacturerid="+ Convert.ToInt16(comboBox4.SelectedValue.ToString());

    comboBox3.DataSource = cls.Select(sqlQuery);
    comboBox3.DataTextField = "name";
    comboBox3.DataValueField = "modelid";
    comboBox3.DataBind();
}

On 1st combobox SelectedIndexChanged event (manufacture combobox) fetch select manufacturerid and then fire this query to populate other combobox ie (model) 在第一个组合框SelectedIndexChanged事件(制造组合框)上,获取选择manufacturerid商ID,然后触发此查询以填充其他组合框,即(模型)

Select modelid,name from modeltable where manufactuerid=@combox1Value

Code Behind Something Like this, i just wrote this code without IDE have a look, might be need some modification 诸如此类的代码背后 ,我只是在没有IDE的情况下编写了此代码,可能需要进行一些修改

private void monufactureComobobox_SelectedIndexChanged(object sender, EventArgs e)
{

 string fecthManufacturerID= manufactureComobobox.selectedItem;
 DataTable dtModel = new DataTable();
 dtModel= ModelComboPopulate(fecthManufacturerID);
 ModelcomboBox.DataSource = dtModel;
 ModelcomboBox.ValueMember = "modelid";
 ModelcomboBox.DisplayMember = "name";

}

public DataTable ModelComboPopulate(string ID)
{
 DataSet ds = new DataSet();
 using (SqlConnection con = new SqlConnection(connection))
 {
    string myquery="Select modelid,name from modeltable where manufacturerid=@combox1Value";
    SqlCommand cmd = new SqlCommand(myquery, con);
    SqlDataAdapter dap = new SqlDataAdapter();
    dap.SelectCommand = cmd;
    cmd.Parameters.Add("@combox1Value", SqlDbType.NVarChar, 15).Value = ID;
    dap.Fill(ds);
    return ds.Tables[0];
  }

}

Use DataSet or DataTable to store the two tables and then populate the strings in a list string strArr . 使用DataSetDataTable存储两个表,然后在列表字符串strArr填充字符串。 Something like below: ( Atleast the logic should work ) 如下所示:( 至少应该逻辑正常

List<string> strArr = new List<string>();
strArr.Items.Clear();
for(int intSubCount = 0; intSubCount < dtTable2.Rows.Count;intSubCount++)
{
   if(MyComboBox.Text.Equals(dtTable2.Rows[intSubCount]["modelid"].ToString()))
   {
       strArr.Add(dtTable2.Rows[intSubCount]["name"].ToString());
   }
}
//
comboBox3.DataSource = strArr;

Or else 要不然

The simple way is to use DataView 简单的方法是使用DataView

  DataView dv =  dtTable1.defaultView;
  dv.RowFilter("modelid = '" + myComboBox.Text + "'");
  //use DataView to populate the Second ComboBox.

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

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