OMG I've been doing this for two days and still i cannot get the solution. This is my scenario:
开发者_运维技巧I have three comboboxes control and I need to load with data from dataset dinamically.
- cbCode1 is populated with the load form.
- cbCode2 is populated when the selected index from cbCode1 changes
- cbCode3 is populates when the selected index from cbCode1 changes
the data is coming from two fields in the datset "description" and "id_code". So I want to associate the value of the description with its id. Because I need to ask for this later..
In the load form I have this:
cbCode1.DataSource = D.Tables[0]; cbCode1.DisplayMember = D.Tables[0].Columns["description"].ColumnName; cbCode1.ValueMember = D.Tables[0].Columns["id_code"].ColumnName;
Now when the selected index from cbcode1 changes:
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;
}
}
}
This code doesn't work i don't know what I'm doing wrong. please help to solve this issue?
Thanks in advance!
A newbie programmer..
I haven't done this before but I have done something similar for a datagridview. What you need to do is use actionlisteners. Basically fill the first combo box and then add an actionlistener to it so it gets called when the selected index changes. Within that actionlistener have it fill the second combo box.
try to do it,like below code...
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";
}
精彩评论