0

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

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

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

4

3 回答 3

2

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

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

并且SelectedIndexChanged如果发生comboBox4

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();
}
于 2012-12-31T07:12:22.227 回答
2

在第一个组合框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];
  }

}
于 2012-12-31T07:13:28.253 回答
2

使用DataSetorDataTable存储这两个表,然后将字符串填充到列表字符串中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.
于 2012-12-31T07:42:56.667 回答