2016-07-15 58 views
0

我想在使用組datagridview總結和展示價值通過總和並顯示在datagridview的數據庫值

string connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Restaurant.accdb;Persist Security Info=False;"; 
string query = "SELECT Column2,SUM(Column1),Receipt No,Date From Total Group By[Column2]"; 
//SELECT Column2 FROM Total GROUP BY [Column2] 
using (OleDbConnection conn = new OleDbConnection(connStr)) 
{ 
    using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn)) 
    { 
     DataSet ds = new DataSet(); 
     if (ds != null) 
     { 
      adapter.Fill(ds); 

      dataGridView1.DataSource = ds.Tables[0]; 

當這種形式加載它的自動關閉沒有顯示任何datagrdview

+0

嘗試調用Fill之前打開連接,也是您的查詢是錯誤的 – apomene

回答

1

月1日修正您的查詢:

string query = "SELECT Column2,SUM(Column1),[Receipt No],Date From Total Group By [Column2],[Receipt No],Date"; 

和2打開連接:

using (OleDbConnection conn = new OleDbConnection(connStr)) 
      { 
       using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn)) 
       { 
        conn.Open(); 
        ... 

編輯: 你的第二查詢應該是:

"SELECT Column3 As [Table No],SUM(Column1) As Price,[Receipt No],Date From Total Where [Receipt No] = '" + textBox1.Text + "' Group By [Column3],[Receipt No],[Date]"; 
+0

當我COLUMN2如表無後寫的又自動關閉,並沒有表現出任何 –

+0

我們括號:如表否] – apomene

+0

嗯良好的工作多了一個我想事情問你有必要寫conn.open BCZ它沒有它已經工作 –

0

請檢查conn.Open();似乎失蹤了。

using (OleDbDataAdapter adapter = new OleDbDataAdapter(query, conn)) 
{ 
    conn.Open(); 
    DataSet ds = new DataSet(); 
    if (ds != null) 
    { 
      adapter.Fill(ds); 

      dataGridView1.DataSource = ds.Tables[0]; 
    ...... 

希望這會有所幫助!