A Combobox Population issue
I am using the following function to populate my ComboBox
public static void FillDropDownList(string Query, System.Windows.Forms.ComboBox DropDownName, string AValue, string Adisplay)
{
string Value = AValue;
string display = Adisplay;
using (var CONN = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Test.accdb;"))
{
CONN.Open();
DataTable dt = new DataTable();
try
{
OleDbCommand cmd = new OleDbCommand(Query, CONN);
OleDbDataReader myReader = cmd.ExecuteReader();
dt.Load(myReader);
}
catch (OleDbException e)
{
MessageBox.Show(e.ToString());
return;
}
DropDownName.DataSource = dt;
DropDownName.ValueMember = Value;
DropDownName.DisplayMember = display;
}
}
And I'm calling it in the load form:
private void Form1_Load(object sender, EventArgs e)
{
FillDropDownList("select CountryCode,Countryname from Countries", comboBox1, "CountryCode", "Countryname");
}
2.After that I'm using the same Function in the event of Selectedindexchanged event to populate another ComboBox:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FillDropDownList("select StateCode,StateName from States Where CountryCode = '" + comboBox1.SelectedValue + "'", comboBox2, "StateCode", "StateName");
}
And for the population of the two combobox's everything works fine however here is the issue I have two combobox's as shown in the pic :
So te first combobox ( country ) loaded in the form perfectly but what I'm trying to do is to make the the second combobox ( the state ) being selected from Database only when I select the country but what happen is that the code runs and the second combobox goes and select from database whatever is the selected index in the first combo is as the pic shows
So what I basically want to do is to make the second Combo being populated only when I select an index from the first combobox.
把你的代码放在SelectionChangeCommitted
而不是SelectedIndexChanged
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
{
FillDropDownList("select StateCode,StateName from States Where CountryCode = '" + comboBox1.SelectedValue + "'", comboBox2, "StateCode", "StateName");
}
I think your problem is being cause because the population of the first combo box fires the event to populate the second one. One way around it is to have something like the following in comboBox1_SelectedIndexChanged(object sender, EventArgs e).
if (comboBox1.Text == string.Empty) { return; }
If that doesn't work try using the "SelectedIndex > 0" or "SelectedValue" property.
As a point of note your code is potentially prone to SQL injection attacks, if anyone was able to modify your combo-box items.
You need to identify wether the item is really changed from Combobox or not by using SelectedIndex
property.
Try This:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex >= 0)
{
FillDropDownList("select StateCode,StateName from States Where CountryCode = '" + comboBox1.SelectedValue + "'", comboBox2, "StateCode", "StateName");
}
}
链接地址: http://www.djcxy.com/p/68598.html
上一篇: 如何从组合框中选择一个值并将其插入到c#中的记录中
下一篇: 一个Combobox人口问题