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();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
loaddata(comboBox1.SelectedValue.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
}
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";
comboBox1.DataSource = dt;
comboBox1.SelectedIndex = -1;
}
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());
}
}
}
}
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;
}
}
}
string newq = "select " + qry + " from tblEmp where [city] = 'Brazil' ";
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();
}
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();
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)
{
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.