**have used the following code for displaying names of my sql db tables in combobox...now i want dat when i click on any of these table names from combo box..my dgv populates with that table's contents...i have gor three tables in my db..lol,datejoin and tble
**

private void Form1_Load(object sender, EventArgs e)
 {
 String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

 SqlConnection con = new SqlConnection(strConnection);
 try
 {

 con.Open();

 SqlCommand sqlCmd = new SqlCommand();

 sqlCmd.Connection = con;
 sqlCmd.CommandType = CommandType.Text;
 sqlCmd.CommandText = "Select table_name from information_schema.tables";

 SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

 DataTable dtRecord = new DataTable();
 sqlDataAdap.Fill(dtRecord);
 comboBox1.DataSource = dtRecord;
 comboBox1.DisplayMember = "TABLE_NAME";
 con.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
   }

**
then i used the following code to populate my dgv but its not working..plzz help :(**

private void PopulateGridView(string tablename)
 {
 String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

 SqlConnection con = new SqlConnection(strConnection);
 try
 {

 con.Open();

 SqlCommand sqlCmd = new SqlCommand();

 sqlCmd.Connection = con;
 sqlCmd.CommandType = CommandType.Text;
 sqlCmd.CommandText = "Select * from " + tablename;

 SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

 DataTable dtRecord = new DataTable();
 sqlDataAdap.Fill(dtRecord);
 dataGridView1.DataSource = dtRecord;

 con.Close();
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 }
 }



 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
 if (comboBox1.SelectedValue != null)
 {
 PopulateGridView(comboBox1.SelectedValue.ToString());
 }

 }

Recommended Answers

All 5 Replies

In debugging have you checked dtRecord contains the expected data after it has been filled on line #20?

It has been a while since I last played with such controls also but for some reason the .Refresh() is floating in my head thinking that needs to be called to update the DGV display. I could be wrong and someone else can confirm that though.

try using SelectedItem instead of SelectedValue in the method comboBox1_SelectedIndexChanged..

Try and let me know..

as guru iyer said try to call the grdLoad method on the combobox selected index changed event and add connection to the sqldataadapter like this

SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd,con);

Regards

There's no need to pass in the connection when the command used to retrieve data stores the connection. Mikey is on the right lines I think.

You definitely need to debug your method and make sure it's being filled.

If no exceptions are being thrown then it seems like there is a data retrieval error (no items returned) or the DGV just needs refreshing (.Refresh() as Mikey pointed out).

PopulateGridView(.SelectedValue.ToString());

SelectedValue will be a DataRowView and not the Text displayed in the combobox.

Use: ((DataRowView)comboBox1.SelectedValue).Row["TABLE_NAME"].ToString()
or I guess you could just use comboBox1.Text.

Also, it would probably be a good idea to dispose of the datagridview data source before re-assigning it to a new DataTable.

        if (dataGridView1.DataSource != null && dataGridView1.DataSource is DataTable)
        {
            ((DataTable)dataGridView1.DataSource).Dispose();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.