如何从不同组合框中的两个不同表中获取数据? [英] how to get data from two different tables in different combobox?
本文介绍了如何从不同组合框中的两个不同表中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表类和类别,我想显示combobox1中的所有类以及combobox2中的所有类别......任何人都可以帮忙吗?
i have two tables class and category, i want to show all the classes in combobox1 and all categories in combobox2... can anyone help?
推荐答案
这里是类ComboBox的示例代码
Here is a Sample Code For Class ComboBox
public DataTable classComboBox()
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("your connection string");
SqlCommand comm = new SqlCommand("Select * from class", conn);
SqlDataAdapter SDA = new SqlDataAdapter(comm);
SDA.SelectCommand = comm;
SDA.Fill(dt);
return dt;
}
这是一个示例代码类别ComboBox
Here is a sample Code For category ComboBox
public DataTable categoryComboBox()
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("your connection string");
SqlCommand comm = new SqlCommand("Select * from category ", conn);
SqlDataAdapter SDA = new SqlDataAdapter(comm);
SDA.SelectCommand = comm;
SDA.Fill(dt);
return dt;
}
你可以调用这样的表格.......
And You Can Call Into Form Like This.......
public void FillClasscombox()
{
DataTable dt = classComboBox();
yourClassCombobox.DataSource = dt;
yourClassCombobox.DisplayMember = "className"; // Here Put Your Display Member Which Yow want display into ComboBox....
yourClassCombobox.ValueMember = "classId";// Here Put Your value Member
}
这里你的catogry的调用方法
Here Your Calling Method Of catogry
public void FillCatogrycombox()
{
DataTable dt = categoryComboBox();
yourClassCombobox.DataSource = dt;
yourClassCombobox.DisplayMember = "category"; // Here Put Your Display Member Which Yow want display into ComboBox....
yourClassCombobox.ValueMember = "categoryId";// Here Put Your value Member
}
最终将FillClasscombox()和FillCatogrycombox()调用到构造函数中
就像这样
Finaly call FillClasscombox() and FillCatogrycombox() into the Constructor
like this
public yourconstructor()
{
InitializeComponent();
FillClasscombox();
FillCatogrycombox();
}
这篇关于如何从不同组合框中的两个不同表中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文