我有三个组合框控件,我需要从数据集中加载数据。
数据来自datset“description”和“id_code”中的两个字段。所以我想将描述的值与其id相关联。因为我需要稍后再问......
在加载表单中我有:
cbCode1.DataSource = D.Tables [0]; cbCode1.DisplayMember = D.Tables [0] .Columns [“description”]。ColumnName; cbCode1.ValueMember = D.Tables [0] .Columns [“id_code”]。ColumnName;
现在,来自cbcode1的选定索引发生变化:
private void cbCode1_SelectedIndexChanged(object sender, EventArgs e)
{
// Get dataset from DB
DataSet D = new DataSet();
D = reason_get(1, 1, 1); // IdStation, Active, Level
// Ensure the cbcode2 is cleared
cbCode2.Items.Clear();
string SelectedValue = cbCode1.SelectedValue.ToString();
foreach (DataRow row in D.Tables[0].Rows)
{
if (row["id_parent_code"].ToString () == SelectedValue )
{
cbCode2.DataSource = D.Tables[0];
cbCode2.DisplayMember = D.Tables[0].Columns["description"].ColumnName;
cbCode2.ValueMember = D.Tables[0].Columns["id_code"].ColumnName;
cbCode2.SelectedIndex = 0;
}
}
}
这段代码不起作用我不知道我做错了什么。请帮忙解决这个问题?
提前致谢!
新手程序员..
答案 0 :(得分:0)
我之前没有这样做,但是我为datagridview做了类似的事情。您需要做的是使用actionlisteners。基本上填充第一个组合框,然后向其添加一个actionlistener,以便在所选索引更改时调用它。在该actionlistener中,它填充了第二个组合框。
答案 1 :(得分:-1)
尝试这样做,如下面的代码......
private void Form1_Load(object sender,EventArgs e) { FillCountry();
}
private void FillCountry()
{
string str = "SELECT CountryID, CountryName FROM Country";
SqlCommand cmd = new SqlCommand(str,con);
//cmd.Connection = con;
//cmd.CommandType = CommandType.Text;
// cmd.CommandText = "SELECT CountryID, CountryName FROM Country";
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
comboBox1.ValueMember = "CountryID";
comboBox1.DisplayMember = "CountryName";
comboBox1.DataSource = objDs.Tables[0];
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue.ToString() != "")
{
int CountryID = Convert.ToInt32(comboBox1.SelectedValue.ToString());
FillStates(CountryID);
comboBox3.SelectedIndex = 0;
}
}
private void FillStates(int countryID)
{
string str = "SELECT StateID, StateName FROM State WHERE CountryID =@CountryID";
SqlCommand cmd = new SqlCommand(str, con);
// SqlConnection con = new SqlConnection(Con);
// cmd.Connection = con;
// string str="SELECT StateID, StateName FROM State WHERE CountryID =@CountryID";
// cmd.Connection = con;
//cmd.CommandType = CommandType.Text;
// cmd.CommandText = "SELECT StateID, StateName FROM State WHERE CountryID =@CountryID";
cmd.Parameters.AddWithValue("@CountryID", countryID);
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
comboBox2.ValueMember = "StateID";
comboBox2.DisplayMember = "StateName";
comboBox2.DataSource = objDs.Tables[0];
}
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
int StateID = Convert.ToInt32(comboBox2.SelectedValue.ToString());
FillCities(StateID);
}
private void FillCities(int stateID)
{
//SqlConnection con = new SqlConnection(str,con);
string str = "SELECT CityID, CityName FROM City WHERE StateID =@StateID";
SqlCommand cmd = new SqlCommand(str,con);
// cmd.Connection = con;
//cmd.CommandType = CommandType.Text;
// cmd.CommandText = "SELECT CityID, CityName FROM City WHERE StateID =@StateID";
cmd.Parameters.AddWithValue("@StateID", stateID);
DataSet objDs = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter();
dAdapter.SelectCommand = cmd;
con.Open();
dAdapter.Fill(objDs);
con.Close();
if (objDs.Tables[0].Rows.Count > 0)
{
comboBox3.DataSource = objDs.Tables[0];
comboBox3.DisplayMember = "CityName";
comboBox3.ValueMember = "CItyID";
}