Combobox和checkedlistbox [英] Combobox and checkedlistbox
问题描述
Hi Buddies,
我有一个组合框,其中包含2个或更多表(tblEmp,tblPdts等),一个选中的列表框和一个datagridview。如果在动态组合框中动态选择表格,我想将选择表的列显示到checkedListBox中。
并且还将所选核对清单的记录显示到dgv中。
示例:
cb = tblEmp
checkedListBox1 = [] FName,[] LName ,[]电话,[]年龄,[]城市
然后如果checkedListBox1 =
[x] FName,[] LName,[]电话,[x]年龄, [x]城市
Datagridview
菲利普21纽约
查尔斯25印度
Maciej 24伦敦
等
所以dgv中的记录取决于我在checkedListBox1中选择的列。
任何建议
我的代码包含两种形式:
public partial class Form1:Form
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
DataTable dt;
public Form1()
{
InitializeComponent();
Fillcombobox();
}
// 更改选择时的tbl名称isn'工作
private void comboBox1_SelectedIndexChanged( object sender,EventArgs e)
{
loaddata(comboBox1.SelectedValue.ToString());
}
private void Form1_Load( object sender,EventArgs e)
{
// 如果页面加载,则为SQL连接
}
private void Fillcombobox()
{
cmd.CommandText =( SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC);
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
comboBox1.DisplayMember = TABLE_NAME;
comboBox1.ValueMember = TABLE_NAME;
// 使用DT中的数据填充组合框
comboBox1.DataSource = dt;
// 清空bzw.清除组合框
comboBox1.SelectedIndex = -1;
}
// 一种方法如果页面打开则执行命令
private void loaddata( string sTableName)
{
string CS =( @ Data Source =; Initial Catalog =; Integrated Security = True);
使用(SqlConnection con = new SqlConnection(S))
{
con.Open();
cmd.CommandText = String .Format( SELECT [column_name] FROM information_schema.columns WHERE table_name ='{0}',sTableName);
SqlCommand command = new SqlCommand(cmd.CommandText,con);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (读者) .Read())
{
checkedListBox1.Items.Add(reader [ 0 ]。ToString());
}
}
}
}
/ * 显示所选选项* /
private void btnShow_Click( object sender,EventArgs e)
{
Form2 f2 = new 窗体2();
int f = 0 ;
string qry = ;
for ( int i = 0 ; i < checkedListBox1.Items.Count; i ++)
{
if (checkedListBox1.GetItemChecked(i))
{
if (f == 1 )
{
qry = qry + , + checkedListBox1 .Items [I]的ToString();
}
if (f == 0 )
{
qry = checkedListBox1.Items [i] .ToString();
f = 1 ;
}
}
}
// 一个新的查询到单击checkbox后显示记录
string newq = 从tblEmp中选择 + qry + [city] ='巴西';
// 点击btn时打开表单
< span class =code-keyword> if (qry!= )
{
f2.copyfun(newq);
f2.Show();
this .Hide();
}
else
{
MessageBox.Show( 选择项目);
}
}
private void btnZurück_Click( object sender,EventArgs e)
{
this 。隐藏();
new EinForm()。ShowDialog();
}
}
}
public Form2()
{
InitializeComponent();
}
// 使用查询表单中的参数加载dgv
private void loadgrid( string qry)
{
string S = ConfigurationManager.ConnectionStrings [ ]。ConnectionString;
SqlConnection con = new SqlConnection(S);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText =(qry);
DataSet d = new DataSet();
// DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(d);
dataGridView1.DataSource = d.Tables [ 0 ];
}
public void copyfun( string qry)
{
loadgrid(qry);
}
私有 void Form2_Load( object sender,EventArgs e)
{
// 如果页面加载,则为SQL连接
string S = ConfigurationManager.ConnectionStrings [ < span class =code-string>]。ConnectionString;
SqlConnection con = new SqlConnection(S);
}
private void btnZurück_Click(< span class =code-keyword> object sender,EventArgs e)
{
this .Hide();
new Form1()。ShowDialog();
}
}
}
我如何获得组合框实现我的意图。
我的sql语句是静态的而不是动态的。
你非常接近找到解决方案;)
我建议更改loaddata()
方法接受表名作为输入参数:
private void loaddata( string sTableName)
{
cmd.CommandText = String .Format( SELECT [column_name] FROM information_schema.columns WHERE table_name ='{0}',sTableName);
// clear CheckedListBox
// 在这里添加CheckBox到CheckedListBox
}
您需要在comboBox1_SelectedIndexChanged
或comboBox1_SelectedValueChanged
方法中调用此程序:
< pre lang =c#> loaddata(comboBox1.Value.ToString());
现在,你应该能够完成你的项目。祝你好运!
Hi Buddies,
I have a combobox which contains 2 or more tables (tblEmp, tblPdts, etc), a checked listbox and a datagridview. I wanna display the columns of a choosen table into the checkedListBox if the table is selected in the combobox dynamically.
And also display the records of a selected checkedlist into dgv.
Example:
cb = tblEmp
checkedListBox1 = []FName, []LName, []Telephone, []Age, []City
Then if checkedListBox1 =
[x]FName, []LName, []Telephone, [x]Age, [x]City
Datagridview
Philip 21 New York
Charles 25 India
Maciej 24 London
etc
So the records in the dgv depends on what column i choose in the checkedListBox1.
Any suggestions
MY CODE CONSISTS oF TWO FORMS:
public partial class Form1: Form
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
DataTable dt;
public Form1()
{
InitializeComponent();
Fillcombobox();
}
//Change tbl name on selection isn't working
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
loaddata(comboBox1.SelectedValue.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
// Sql Connection if page loads
}
private void Fillcombobox()
{
cmd.CommandText = ("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES ORDER BY TABLE_NAME ASC");
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
dt = new DataTable();
adapter.Fill(dt);
comboBox1.DisplayMember = "TABLE_NAME";
comboBox1.ValueMember = "TABLE_NAME";
//Fill combobox with data in DT
comboBox1.DataSource = dt;
// Empty bzw. clear the combobox
comboBox1.SelectedIndex = -1;
}
// A methode to execute a command if page opens
private void loaddata(string sTableName)
{
string CS = (@"Data Source=;Initial Catalog=;Integrated Security=True");
using (SqlConnection con = new SqlConnection(S))
{
con.Open();
cmd.CommandText = String.Format("SELECT [column_name] FROM information_schema.columns WHERE table_name = '{0}'", sTableName);
SqlCommand command = new SqlCommand(cmd.CommandText, con);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
checkedListBox1.Items.Add(reader[0].ToString());
}
}
}
}
/* Display selected options*/
private void btnShow_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
int f = 0;
string qry = "";
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if(checkedListBox1.GetItemChecked(i))
{
if(f == 1)
{
qry = qry + "," + checkedListBox1.Items[i].ToString();
}
if (f == 0)
{
qry = checkedListBox1.Items[i].ToString();
f = 1;
}
}
}
// A new query to to display the records after checkedbox is clicked
string newq = "select " + qry + " from tblEmp where [city] = 'Brazil' ";
// Open form two when btn is clicked
if (qry != "")
{
f2.copyfun(newq);
f2.Show();
this.Hide();
}
else
{
MessageBox.Show("Select an item");
}
}
private void btnZurück_Click(object sender, EventArgs e)
{
this.Hide();
new EinForm().ShowDialog();
}
}
}
public Form2()
{
InitializeComponent();
}
// Load dgv with a parameter from the query form
private void loadgrid(string qry)
{
string S = ConfigurationManager.ConnectionStrings[""].ConnectionString;
SqlConnection con = new SqlConnection(S);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = (qry);
DataSet d = new DataSet();
//DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(d);
dataGridView1.DataSource = d.Tables[0];
}
public void copyfun(string qry)
{
loadgrid(qry);
}
private void Form2_Load(object sender, EventArgs e)
{
// Sql Connection if page loads
string S = ConfigurationManager.ConnectionStrings[""].ConnectionString;
SqlConnection con = new SqlConnection(S);
}
private void btnZurück_Click(object sender, EventArgs e)
{
this.Hide();
new Form1().ShowDialog();
}
}
}
How do i get the combobox to implement my intention.
And my sql statement is static instead of making it dynamic.
You're very close to find solution ;)
I'd suggest to changeloaddata()
method to accept table name as input parameter:
private void loaddata(string sTableName) { cmd.CommandText = String.Format("SELECT [column_name] FROM information_schema.columns WHERE table_name = '{0}'", sTableName); //clear CheckedListBox //add CheckBoxes to CheckedListBox here }
You need to call this procedure insidecomboBox1_SelectedIndexChanged
orcomboBox1_SelectedValueChanged
method:
loaddata(comboBox1.Value.ToString());
Now, you should be able to finish your project. Good luck!
这篇关于Combobox和checkedlistbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!