我是一名学生,也是编程的新手,我有两个组合框,combobox1和combobox2 combobox1包含移动公司的像诺基亚,三星,HTC和combobox2包含三星,s3等移动模型,我想排序两个组合框我的意思是当我点击组合框中的诺基亚时,所有nokia模型应该在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
我希望如果我在第一个组合框中选择nokia,那么第二个组合框将选择所有的制造商= 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";
}
}
但每次添加新模型时我都要更新三星字符串,所以我决定使用表格以便我可以更新
答案 0 :(得分: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();
}
答案 1 :(得分: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];
}
}
答案 2 :(得分:2)
使用DataSet
或DataTable
存储两个表,然后在列表字符串strArr
中填充字符串。如下所示:( Atleast逻辑应该工作)
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.