2013-03-13 92 views
0

我已經使用以下代碼在組合框中顯示我的SQL數據庫表的名稱。如何通過從組合框中選擇任何表來填充DataGridView

現在我希望當我從組合框中單擊任何這些表名時,我的DGV將填充該表的內容。

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

然後我用下面的代碼填充我的DGV,但它不工作;請幫忙。

private void PopulateGridView() 
{ 
    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 " + comboBox1.SelectedText; 

     SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd); 

     DataTable dtRecord = new DataTable(); 
     sqlDataAdap.Fill(dtRecord); 
     dataGridView1.AutoGenerateColumns = true; 
     dataGridView1.DataSource = dtRecord; 
     dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 
     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()); 
    } 
} 
+0

您是否嘗試調試您的代碼?說「它不工作」是什麼意思?你有沒有收到任何錯誤信息或什麼? – 2013-03-13 06:46:00

+0

爲什麼當該方法是無參數時,用參數調用'PopulateGridView'方法? – 2013-03-13 06:48:04

+0

我hav調試我的代碼...組合框顯示我的sql數據庫中的表名...但我的dgv沒有填充表中的內容我從組合框中選擇..plzz幫助 – 2013-03-13 06:50:04

回答

1

只是試試這個: 更新的形式加載你的代碼如下:

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"; 
     comboBox1.ValueMember = "table_name"; 
     con.Close(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
} 

所以基本上你添加一個額外的行comboBox1.ValueMember = "table_name";

並使你的PopulateGridView方法如下:

private void PopulateGridView(string tblName) 
     { 
      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 " + tblName; 

       SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd); 

       DataTable dtRecord = new DataTable(); 
       sqlDataAdap.Fill(dtRecord); 
       dataGridView1.AutoGenerateColumns = true; 
       dataGridView1.DataSource = dtRecord; 
       dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 
       con.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 

它必須工作。 此外,我看到,您正在創建SqlConnection對象,包括SqlCommandSqlDataAdapter

嘗試包裹起來的靜態方法即

- public static SqlConnection OpenConnection() 
- public static DataTable ExecuteSelectQuery(string Query) 
- public static bool ExecuteModifyQuery(string Query) 

嘗試,你可以寫的代碼量更少。

+0

我的dgv仍然是空白的:( – 2013-03-13 07:17:55

+0

你遇到一些異常?因爲這對我有用。 – 2013-03-13 07:19:38

+0

沒有什麼沒有錯誤沒有例外.. :(如果psb可以發佈整個代碼,你已經嘗試? – 2013-03-13 07:21:09

1

試試這個:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox1.SelectedValue != null) 
     { 
      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 " + comboBox1.SelectedValue; 
       SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd); 

       DataTable dtRecord = new DataTable(); 
       sqlDataAdap.Fill(dtRecord); 
       dataGridView1.AutoGenerateColumns = true; 
       dataGridView1.DataSource = dtRecord; 
       dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells); 
       con.Close(); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 
     } 
    } 
+0

thnks的代碼...但仍然我的dgv是空的:( – 2013-03-13 06:57:19

+0

@MayuriKoul你檢查數據表(dtrecord) – 2013-03-13 07:02:28

+0

如何檢查?... :( – 2013-03-13 07:05:02

相關問題