ComboBox_SelectedIndexChanged执行到第二个ComboBox [英] ComboBox_SelectedIndexChanged execute to seconde ComboBox
问题描述
大家好,
关于将数据从数据库加载到第二个comboBox的问题,我没有什么问题,
这是创建它的代码,
1)
< pre lang ="cs"> private void LoadCategory()
{
试试
{
this.cnn.cmdOpen(cmdForm);
cmdForm.CommandText =& quot;从P_ProdList中选择不同的P_类别WHERE P_Code OR BY BY P_Category" ;;
OleDbDataReader DR = cmdForm.ExecuteReader();
cmb1.Items.Clear();
cmb1.Items.Add(&选择类别& quot;);
而(DR.Read())
{
cmb1.Items.Add(DR [& quot; P_Category& quot;]]);
}
cmb1.SelectedIndex = 0;
cnn.cn.Close();
cmdForm.Dispose();
}
catch(ex ex例外)
{
MessageBox.Show(例如Message,& quot; SMS& quot ;, MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}</pre>
< pre lang ="cs">私有无效cmb1_SelectedIndexChanged(对象发送者,EventArgs e)
{
cboselectdata();
}
私有void cboselectdata()
{
试试
{
this.cnn.cmdOpen(cmdForm);
cmdForm.CommandText ="从P_ProdList中选择P_ProdName,其中P_Category ='& quot; + cmb1.Text +&'& quot ;;
OleDbDataReader READ = cmdForm.ExecuteReader();
而(READ.Read())
{
//txt1.Text = READ [0] .ToString();
//cmb2.Text =(READ [])
//cmb2.Items.Add(READ [& quot; P_ProdName& quot;]]);
}
cnn.cn.Close();
cmdForm.Dispose();
READ.Dispose();
}
catch(ex ex例外)
{
MessageBox.Show(例如Message,& quot; SMS& quot ;, MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}</pre>
我想从数据库中加载所有数据,在一个类别中类别示例下的Products拥有3个产品,我希望将产品加载到comboBox(产品)的每个选定类别,应该是产品更改,但就我而言不能改变..有人可以帮助我.
联系电子邮件. mlextech@gmail.com
Hello Everyone,
I have little bit problem regarding on loading the data from database to second comboBox,
here the code the that made it,
1)
<pre lang="cs">private void LoadCategory()
{
try
{
this.cnn.cmdOpen(cmdForm);
cmdForm.CommandText = "SELECT Distinct P_Category from P_ProdList WHERE P_Code ORDER BY P_Category";
OleDbDataReader DR = cmdForm.ExecuteReader();
cmb1.Items.Clear();
cmb1.Items.Add("Select Categories");
while (DR.Read())
{
cmb1.Items.Add(DR["P_Category"]);
}
cmb1.SelectedIndex = 0;
cnn.cn.Close();
cmdForm.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SMS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}</pre>
<pre lang="cs">private void cmb1_SelectedIndexChanged(object sender, EventArgs e)
{
cboselectdata();
}
private void cboselectdata()
{
try
{
this.cnn.cmdOpen(cmdForm);
cmdForm.CommandText = "SELECT P_ProdName FROM P_ProdList WHERE P_Category='" + cmb1.Text + "'";
OleDbDataReader READ = cmdForm.ExecuteReader();
while (READ.Read())
{
// txt1.Text = READ[0].ToString();
//cmb2.Text =(READ[])
// cmb2.Items.Add(READ["P_ProdName"]);
}
cnn.cn.Close();
cmdForm.Dispose();
READ.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "SMS", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}</pre>
I want to load all the data from the database the Products which is the under the categories example in one category have 3 product i want that product load to the comboBox (products) every categories selected it should be the products change but in my case doesn''t change..somebody can help me please.
contact emails. mlextech@gmail.com
thanks you very much.
推荐答案
我无法用您的描述中给出的代码找出问题,但是肯定会告诉您如何执行.
请按照以下步骤操作:
1.将类别(来自类别表)加载到类别组合框中
我假设您在表中有一个类别ID
加载时,请勿放置"Item.Add"
而是使用数据库中的结果填充数据表
然后编码为:
I would not be able to figure out the issue with the code given in your description but surely would tell you how to do it.
Please follow the following steps:
1. Load categories (from category table) in to the categories combo box
I assume that you have a category ID in the table
While loading, do not put "Item.Add"
Instead fill a data table with the results from the database
Then code as:
For windows application;
catgoriescombobox.displaymemember = categoryname
catgoriescombobox.valuemember = categoryid
catgoriescombobox.datasource = datatable
For web applications;
catgoriescombobox.datatextfield = categoryname
catgoriescombobox.datavaluefield = categoryid
catgoriescombobox.datasource = datatable
catgoriescombobox.databind()
2.在所选索引上更改为类别组合框;
int catid = Convert.ToInt32(this.categorriescombobox.selecteditem.value)
从categoryid = catid
的产品中选择*
从上述查询的结果中加载数据表
然后编码为:
2. On selected index changed for categories combo box;
int catid = Convert.ToInt32(this.categorriescombobox.selecteditem.value)
select * from products where categoryid = catid
load the datatable from the results of the above query
Then code as:
For windows application;
productscombobox.displaymemember = categoryname
productscombobox.valuemember = categoryid
productscombobox.datasource = datatable
For web applications;
productscombobox.datatextfield = categoryname
productscombobox.datavaluefield = categoryid
productscombobox.datasource = datatable
productscombobox.databind()
希望对您有帮助
Hope this helps
这篇关于ComboBox_SelectedIndexChanged执行到第二个ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!