使用C#在Windows窗体中级联comboBox [英] cascading comboBox in windows form using c#
问题描述
我正在尝试从Windows窗体应用程序中的同一张表更改的combobox1 selectedText上填充Combobox2。我正在使用sql serevr 2008数据库。我无法在combobox中选择的文本更改后填充combobox2。
I am trying to Fill Combobox2 on combobox1 selectedText changed from the same table in windows form application. I am using sql serevr 2008 database. I am unable to fill combobox2 on combobox selected text changed.
这是我尝试过的内容:
private void Purchase_Load(object sender, EventArgs e)
{
fillName();
comboBoxName.SelectedIndex = -1;
}
private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBoxName.SelectedText != "")
{
fillMake();
}
}
private void fillName()
{
SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True");
con.Open();
string str = "Select Item_Name from Item";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter adp = new SqlDataAdapter(str, con);
DataTable dtItem = new DataTable();
adp.Fill(dtItem);
cmd.ExecuteNonQuery();
comboBoxName.DataSource = dtItem;
comboBoxName.DisplayMember = "Item_Name";
comboBoxName.ValueMember = "Item_Make";
}
private void fillMake()
{
SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True");
con.Open();
string str = "Select Item_Make from Item Where Item_Name='" + comboBoxName.SelectedText + "'";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter adp = new SqlDataAdapter(str, con);
DataTable dtItem = new DataTable();
adp.Fill(dtItem);
cmd.ExecuteNonQuery();
comboBoxName.DataSource = dtItem;
comboBoxName.DisplayMember = "Item_Make";
comboBoxName.ValueMember = "Item_Name";
comboBoxName.SelectedIndex = -1;
comboBoxName.Text = "Select";
}
项目的Sql服务器表
Item_Code Item_Name Item_Make Item_Price UnitofMeasurement
Cable anchor 45.0000 meter
Cable polycab 30.0000 meter
Button anchor 15.0000 unit
Button havells 20.0000 unit
Switch cona 70.0000 unit
我一直在寻找解决方案,但很不幸。
,请帮帮我。
预先感谢。
I have searched for solution but was unfortunate. please help me out. Thanks in advance.
推荐答案
要弄清楚您要执行的操作有点困难,但是听起来您好像要填充第二个组合框( comboBoxMake
??),具体取决于在 comboBoxName
中选择的内容。我以这个假设为基础。抱歉,如果我遇到这个错误。
It's a little difficult to figure out what you're trying to do, but it sounds like you are trying to populate a second combo box (comboBoxMake
?) depending on what is selected in comboBoxName
. I am basing this answer on that assumption. Apologies if I have this wrong.
这段代码中有很多事情需要注意。我们先来看 fillName()
。
There are lot of things that need attention in this code. Let's look at fillName()
first.
private void fillName()
{
SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True");
con.Open();
string str = "Select Item_Name from Item";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter adp = new SqlDataAdapter(str, con);
DataTable dtItem = new DataTable();
adp.Fill(dtItem);
cmd.ExecuteNonQuery();
comboBoxName.DataSource = dtItem;
comboBoxName.DisplayMember = "Item_Name";
comboBoxName.ValueMember = "Item_Make";
}
您需要 Dispose()
您的数据库对象。可以使用使用{..}
块很干净地完成此操作。
You need to Dispose()
your database objects. This can be accomplished pretty cleanly with using { .. }
blocks.
您无需手动打开连接;用数据适配器
填充表会自动执行此操作。
You don't need to manually open the connection; filling the table with the data adapter does this automatically.
您不需要调用 ExecuteNonQuery()
。
您应该使用 SqlDataAdapter
带有命令对象的构造函数重载,因为您已经
You should use the SqlDataAdapter
constructor overload that takes a command object, since you have already manually created the command.
最后,根据我对目标的假设,我在查询中添加了 distinct
因此它只会获得唯一的 Item_Name
s。
Finally, based on my assumption of your goal I have added a distinct
to your query so it only gets the unique Item_Name
s.
private void fillName()
{
string str = "Select distinct Item_Name from Item";
using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(str, con))
{
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
DataTable dtItem = new DataTable();
adp.Fill(dtItem);
comboBoxName.DataSource = dtItem;
comboBoxName.DisplayMember = "Item_Name";
comboBoxName.ValueMember = "Item_Name";
}
}
}
}
到 fillMake()
。我上面已经提到了同样的建议。另外:
On to fillMake()
. The same suggestions apply that I noted above. Additionally:
参数化您的SQL。 参数化您的SQL 。这不仅比串联SQL更安全,而且更加干净。认真阅读有关SQL注入的信息: http://en.wikipedia.org/wiki/SQL_injection
Parameterize your SQL. Parameterize your SQL. Not only is this far, far safer than concatenating your SQL together, it is much cleaner. Seriously, read about SQL injection: http://en.wikipedia.org/wiki/SQL_injection
原始帖子中的 fillMake()
方法似乎正在填充 comboBoxName
。这样对吗?您提到了两个组合框,但是您的代码仅引用了一个。我假设您的意思是在此处填充另一个组合框( comboBoxMake
?):
The fillMake()
method in your original post seems to be repopulating comboBoxName
. Is this correct? You mention two combo boxes but your code only references one. I am assuming you mean to populate another combo box (comboBoxMake
?) here:
private void fillMake()
{
string str = "Select Item_Make from Item Where Item_Name = @item_name";
using (SqlConnection con = new SqlConnection(@"Data Source=ashish-pc\;Initial Catalog=HMS;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(str, con))
{
cmd.Parameters.AddWithValue("@item_name", comboBoxName.Text);
using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
{
DataTable dtItem = new DataTable();
adp.Fill(dtItem);
comboBoxMake.DataSource = dtItem;
comboBoxMake.DisplayMember = "Item_Make";
comboBoxMake.ValueMember = "Item_Make";
comboBoxMake.SelectedIndex = -1;
comboBoxMake.Text = "Select";
}
}
}
}
最后,请更改事件处理程序中的代码,以便它查看 Text
而不是 SelectedText
属性:
Lastly, change the code in the event handler so it looks at the Text
rather than the SelectedText
property:
private void comboBoxName_SelectedIndexChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(comboBoxName.Text)) // Text instead of SelectedText
{
fillMake();
}
}
这篇关于使用C#在Windows窗体中级联comboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!