本文介绍了如何从同一个表的两列中为同一表单上的两个组合框设置数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个带有两个组合框的Windows表单应用程序。我希望combobox1从表BIn中的名为Type的列中获取其内容,并使用combobox2从表BIn中的名为PN的列中获取其内容。我已经解决了单输入的情况。但这是双输入。我似乎无法成功扩展单输入解决方案。



我尝试过:



public B()

{

InitializeComponent() ;


SqlConnection con = new SqlConnection(Data Source = PV10 \\LOCALSERVER; Initial Catalog = SmallSoftwareDB; Integrated Security = True; Pooling = False);

con.Open();

string sPn =从BIn中选择PN;

string sTp =从BIn中选择类型;

SqlCommand CsPn = new SqlCommand(sPn,con);

SqlCommand CsTp = new SqlCommand(sTp,con);



SqlDataReader mDr;

SqlDataReader CmDr;

mDr = CsPn.ExecuteReader();



while(mDr.Read())

{

comboBox2.Items.Add(mDr [PN]。ToString());

}



使用(SqlDataAdapter da = new SqlDataAdapter(CsTp))

{

DataTable dt = new DataTable();

da.Fill(dt);

comboBox1.DataSource = dt;

comboBox1。 DisplayMember =Type;

}

}

I have designed a Windows form application with two comboboxes. I want combobox1 to take its contents from a column named "Type" in table "BIn" and combobox2 to take its contents from a column named "PN" in table "BIn". I already solved the case of a single input.but this is double input.I can't seem to be able to successfully extend the single input solution.

What I have tried:

public B()
{
InitializeComponent();

SqlConnection con = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False");
con.Open();
string sPn = "select PN from BIn";
string sTp = "select Type from BIn";
SqlCommand CsPn = new SqlCommand(sPn, con);
SqlCommand CsTp = new SqlCommand(sTp, con);

SqlDataReader mDr;
SqlDataReader CmDr;
mDr = CsPn.ExecuteReader();

while (mDr.Read())
{
comboBox2.Items.Add(mDr["PN"].ToString());
}

using (SqlDataAdapter da = new SqlDataAdapter(CsTp))
{
DataTable dt = new DataTable();
da.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Type";
}
}

推荐答案

string query = "select PN,Type from BIn";
           SqlCommand cmd = new SqlCommand(query, con);
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           comboBox1.DisplayMember = "Type";
           comboBox1.DataSource = dt;
           comboBox2.DisplayMember = "PN";
           comboBox2.DataSource = dt;


这篇关于如何从同一个表的两列中为同一表单上的两个组合框设置数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 15:04