如何匹配两个组合框 [英] How to match two comboboxes
本文介绍了如何匹配两个组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I’ve two comboboxes which should contain two different informations.
1.cb1: select table_name from information_schema.tables (this display multiple tables)
2.cb2: should populate it with a column name from the selectedvalue at cb1
以下代码正在填充我的cb1(组合框)。
This code below is working and populating my cb1 (combobox).
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString;
SqlConnection con = new SqlConnection(C);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC");
try
{
// Open connection, Save the results in the DT and execute the spProc & fill it in the DT
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
cbTbl.DisplayMember = "TABLE_NAME";
cbTbl.ValueMember = "TABLE_NAME";
//Fill combobox with data in DT
cbTbl.DataSource = dt;
// Empty bzw. clear the combobox
cbTbl.SelectedIndex = -1;
下一个代码几乎是正确的并且还填充cb2但是带有System.Data.DataView。
怎么了?
我想在cb2中获取cb1中所选表格的特定列
The next code is almost correct and also populating cb2 BUT with System.Data.DataView.
What's wrong?
I want to get in cb2 a specific column of the selected table in cb1
private void comboBox1_SelectedIndexValue(object sender, EventArgs e)
{
if (cbTbl.SelectedValue != null)
{
string C = ConfigurationManager.ConnectionStrings[""].ConnectionString; SqlConnection con = new SqlConnection(C);
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandText = (" SELECT [Projektdefinition] FROM " + cbTbl.SelectedValue.ToString() + "");
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
try
{
con.Open();
dt = new DataTable(); // If i debug there are no records in there
sqlDataAdap.Fill(dt);
comboBox1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
con.Close();
}
}
Thnx
Thnx
推荐答案
添加此行
Add this line
comboBox1.DisplayMember = "Projektdefinition"; // add this line
comboBox1.ValueMember = "Projektdefinition";// add this line
comboBox1.DataSource = dt;
尝试使用
try with
sqlCmd.CommandText = ("SELECT [Projektdefinition] FROM " + ((DataRowView)cbTbl.SelectedItem).Row["TABLE_NAME"].ToString());
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
try
{
con.Open();
DataTable datatable= new DataTable();
sqlDataAdap.Fill(datatable);
comboBox1.DataSource = datatable;
comboBox1.DisplayMember = "Projektdefinition";
comboBox1.ValueMember = "Projektdefinition";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
这篇关于如何匹配两个组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文