通过从组合框中选择任意表来填充datagridview

问题描述:

我已经使用下面的代码在组合框中显示我的SQL数据库表的名称。现在我希望当我点击组合框中的任何这些表格名称时,我的DataGridView将填充该表格的内容。通过从组合框中选择任意表来填充datagridview

我在我的数据库中有三个表。

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); 
     } 
    } 
    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()); 
     } 

    } 
} 

}

这里是我的表(datejoin)

rahul sharma 12-1-2011 
suresh chand 23-4-2012 
prachi shukla 13-2-2011 
siddharth malhotra 25-9-2012 
saina chopra 12-8-2011 
amit mehta 20-3-2012 
+0

有啥这个问题? – Zaki 2013-03-11 10:24:30

+0

的问题是笏将代码填充我的dgv与表中的内容我选择从组合框 – 2013-03-11 10:26:04

订阅组合框的SelectedIndexChanged事件和检索组合框中选择表名。从那里运行查询以从表中检索记录并将结果绑定到datagridview。

样品:

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

    SqlConnection con = new SqlConnection(strConnection); 
    con.Open(); 

    string tableName = comboBox1.SelectedText; 

    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); 
    dgv.DataSource = dtRecord; 
    con.Close(); 
} 
+0

谢谢.. :)但plz可以编写的代码..新的C#.. PLZ PLZ PLZ如果PSB – 2013-03-11 10:28:05

+0

看我的编辑。此外,您可能希望通过创建不同的类来从数据库中检索记录来分隔数据层。 – 2013-03-11 10:35:31

+0

它在sqlDataAdap.Fill(dtRecord)上显示错误; ....'from'附近的语法不正确。 – 2013-03-11 10:44:49

您将有组合框SelectedIndexChange一个事件,并在你的函数传递表名称给你的函数

private void ComboBox1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
    RebindGridView(ComboBox1.SelectedValue); 
} 

然后:

private void RebindGridView(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); 
       gridview1.DataSource = dtRecords; 
       gridview1.DataBind(); 
       con.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
} 
+0

thnks,但它不工作... :( – 2013-03-11 10:37:52

+0

什么错误,你会得到?也看到我的编辑忘记了从和表名之间的空间......现在是正确的 – Zaki 2013-03-11 10:38:07

+0

我在哪里需要定义databind()? – 2013-03-11 10:43:02