【问题标题】:Populate ComboBox2 depending on Combobox1 selected item根据 Combobox1 选定项填充 ComboBox2
【发布时间】:2012-12-31 07:01:51
【问题描述】:

我是一名学生,并且是编程新手,我有两个组合框,combobox1 和 combobox2 combobox1 包含诺基亚、三星、htc 等移动公司,combobox2 包含三星、s3 等移动型号,我想对这两个进行排序组合框我的意思是当我点击组合框1中的诺基亚时,诺基亚的所有型号都应该在组合框2的列表中可见,所以我决定使用外键关系

          Manufacturer -table
        - manufacturerid (primary key)
        - name

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

数据示例:

制造商表

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

模型表

       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

我希望如果我在第一个组合框中选择诺基亚,那么第二个组合框将选择所有制造 ID = 1 的型号 用什么?我怎样才能做到这一点? 私下我正在使用

 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";
       }
  }

但我每次添加新模型时都必须更新三星字符串,所以我决定使用表格以便我可以更新它

【问题讨论】:

  • 上面的例子我为一家公司使用了整列,但出现了一些问题,所以我决定使用这个外键
  • 请看我的回答
  • 您想填充组合框还是要更新??
  • 您到底想要什么?看来您正在尝试的代码可以正常工作。您要优化代码吗?
  • 我想用正确的模型填充

标签: c#


【解决方案1】:

在第一个组合框SelectedIndexChanged 事件(制造组合框) 获取选择manufacturerid 然后触发此查询以填充其他组合框,即(模型)

Select modelid,name from modeltable where manufactuerid=@combox1Value

代码隐藏 像这样的,我只是在没有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];
  }

}

【讨论】:

  • paaji,我要直接写这个吗? private void comboBox4_SelectedIndexChanged(object sender, EventArgs e) { Select modelid,name from modeltable where manufactuerid=@combox1Value }
  • @shariq_khan:Bhai,你想填充组合框还是更新数据?
  • paaji 我想填充组合框。当combobox1 nokia 被选中时,所有具有制造商ID 1 的诺基亚型号都将填充到combobox2 中。像那样
  • 既然 'WindowsFormsApplication2.Form6.comboBox6_SelectedIndexChanged(object, System.EventArgs)' 返回 void,return 关键字后面不能跟对象表达式这是什么先生?我不知道这种错误的含义
  • @shariq_khan:删除这一行return ds.table[0]
【解决方案2】:

使用DataSetDataTable 存储两个表,然后将字符串填充到列表字符串strArr 中。如下所示:(至少逻辑应该起作用

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;

否则

简单的方法是使用DataView

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

【讨论】:

【解决方案3】:

包含一个从数据库中检索数据的函数,如下所示

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

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();
   }
 }

并且在comboBox4SelectedIndexChanged事件中

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();
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 2017-02-09
  • 1970-01-01
相关资源
最近更新 更多