Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
2.67/5 (2 votes)
See more:
I have a combo box and a datagridview. I load the combobox with 2 tables in my database. I want to select the table name which generate from the combobox and display the table content in the gridview.
I got stuck i don't know how to populate gridview. Can anybody help?

private void viewTableButton_Click(object sender, EventArgs e)
      {
         using (SqlConnection sqlConn = new SqlConnection("Data Source=;Initial Catalog=" + "Integrated Security=SSPI"))
          {
              SqlCommand sqlCmd = sqlConn.CreateCommand();

              //using sys.tables to call all table in database
              sqlCmd.CommandText = ("Select * From sys.tables");
              sqlConn.Open();
              SqlDataReader sqlDR = sqlCmd.ExecuteReader();
              while (sqlDR.Read())
              {
                  tablesCombobox.Items.Add((string)sqlDR[0]);
              }
              sqlDR.Dispose();
              sqlCmd.Dispose();

              for(int index = 0; index <= tablesCombobox.Selectedindex
              {//What code  i need to put in here to populate the gridview
               //which selected from combobox
              }

          }
      }
Posted
Comments
Sergey Alexandrovich Kryukov 13-Mar-12 17:55pm    
Not clear what "ComboBox". There are different types under this name. Please tag your UI library and/or application type: WPF? Forms? Silverlight? ASP.NET?

--SA
P.S.: Yes, I can guess but I don't want. The question should be correctly put.

Try this code------------

private void DisplayTableData_Click(object sender, EventArgs e)
{
        SqlConnection con = new SqlConnection();
         Try
        {
          if(!string.isNullOrEmpty(cbTable.SelectedText))
            {
                    con.ConnectionString="Database ConnectionString";
                    SqlCommand sqlCmd = new SqlCommand("Select * From " + cbTable.SelectedText, sqlCon);
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand=cmd;
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                   dgvTableData.DataSource = dt.DefaultView;
            }
            else
             {
                    MessageBox.Show("No Table Selected...");
             }
         }
        Catch(Exception ex)
        {
          MessageBox.Show(ex.Message);
        }         
}
 
Share this answer
 
v2
Comments
CHill60 24-Jun-13 12:07pm    
The question is over a year old and already had solutions posted. Your solution isn't that much different from Solution 1
C#
private void viewTableButton_Click(object sender, EventArgs e)
 {
     using (SqlConnection sqlConn = new SqlConnection("Data Source=;Initial Catalog=" + "Integrated Security=SSPI"))
     {
           SqlCommand sqlCmd = sqlConn.CreateCommand();
 
           //using sys.tables to call all table in database
           sqlCmd.CommandText = ("Select * From sys.tables");
           sqlConn.Open();
           SqlDataReader sqlDR = sqlCmd.ExecuteReader();
           while (sqlDR.Read())
           {
               tablesCombobox.Items.Add((string)sqlDR[0]);
           }
           sqlDR.Dispose();
           sqlCmd.Dispose();
                
           //Try This
           if (comboBox1.SelectedIndex != -1)
           {
               sqlCmd.CommandText = ("Select * From "+ comboBox1.SelectedText);
               SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
               DataSet ds = new DataSet();
               da.Fill(ds);
               datagridview1.DataSource = ds.Tables[0];
           }
     }
}
 
Share this answer
 
Comments
chaupha 14-Mar-12 10:40am    
I tried your code, but when i selected a table from combox and click the button, an error message stating that systax error near from which is this line
sqlCmd.CommandText = ("Select * From " +comboBox1.SelectedText);

Is there something wrong with the query, can you help me with this query?

thanks a lot for your help.

if (comboBox1.SelectedIndex != -1)
{
sqlCmd.CommandText = ("Select * From "+ comboBox1.SelectedText);
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataSet ds = new DataSet();
da.Fill(ds);
datagridview1.DataSource = ds.Tables[0];
}
Mantu Singh 25-Apr-12 3:41am    
try combobox1.selecteditem.tostring() this may work or may be some prob with connection string.......
You need to take a datagidview say datagridview1 and
apply the following

1)generate the combobox_selectedindexchange event and write the following code


C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {


SqlConnection sqlConn = new SqlConnection("Data Source=;Initial Catalog=" + "Integrated Security=SSPI");

SqlCommand sqlCmd = sqlConn.CreateCommand();
 
               
                sqlCmd.CommandText = ("Select * From "+ comboBox1.SelectedText);
SqlDataAdapter da=new SqlDataAdapter(sqlCmd);
DataSet ds=new DataSet();
da.Fill(ds);

datagridview1.DataSource=ds.Tables[0];

        }
 
Share this answer
 
Comments
chaupha 14-Mar-12 10:37am    
I generate the combobox_selectedindexchange event, and copy the code, but there is nothing appear in the gridview when i select a table in combox. You have any other solution.

Thanks a lot for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900