2016-10-10 57 views
0

選擇的日期當我選擇從從的DateTimePicker日期和todate當時的datagridview顯示在datagridview的數據給我看空的數據,代碼寫按鈕點擊EVET,這裏IM使用access數據庫價值不列入在datagridview的節目時的DateTimePicker

DateTime startT = new DateTime(); 
       DateTime endT = new DateTime(); 
       startT = dateTimePicker1.Value.Date; 
       endT = dateTimePicker2.Value.Date; 
       if (startT.Date > endT.Date) 
       { 
        MessageBox.Show("To Date Cannot be greater than Start Date"); 
       } 
       else 
       { 
        string connetionString = null; 
        connetionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString; 
        con.ConnectionString = connetionString; 

        DataSet ds = new DataSet(); 
        string sql = "SELECT Medicine_name,sum(Medicine_count) as Medicine_count,To_Date from Medicine_count_info where [To_Date] Between #" + startT.ToString("dd'/'MM'/'yyyy") + "#And #" + endT.ToString("dd'/'MM'/'yyyy") + "#group by Medicine_name,Medicine_count,To_Date order by Medicine_count desc "; 
        OleDbConnection connection = new OleDbConnection(connetionString); 
        OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection); 
        ds = new DataSet(); 
        connection.Open(); 
        dataadapter.Fill(ds); 
        connection.Close(); 
        dataGridView1.DataSource = ds; 
        dataGridView1.DataMember = ds.Tables[0].TableName; 
        dataGridView1.Refresh(); 
} 

enter image description here enter image description here

+0

如果'To_Date'是日期列,則不需要折磨日期範圍。使用SQL參數並傳遞一個日期。你也應該處理連接和命令對象......並且如果你堅持DataTable,你可以應用一個過濾器而不是運行一個新的查詢 – Plutonix

回答

0

首先,在你的選擇,你在"#And #"

此外,assumi缺少空間ng [To_Date]是一個日期時間字段,問題可能在於如何構建將字符串作爲數據傳遞的select子句。使用日期時間字段,當然總是使用參數化查詢。類似這樣的:

string sql = "SELECT Medicine_name,sum(Medicine_count) as Medicine_count,To_Date " + 
"FROM Medicine_count_info " + 
"WHERE [To_Date] Between @startDate And @endDate " + 
"GROUP BY Medicine_name,Medicine_count,To_Date " + 
"ORDER BY Medicine_count desc "; 
OleDbDataAdapter dataadapter = new OleDbDataAdapter(sql, connection); 
dataadapter.SelectCommand.Parameters.AddWithValue("@startDate", startT); 
dataadapter.SelectCommand.Parameters.AddWithValue("@startDate", endT); 
ds = new DataSet(); 
dataadapter.Fill(ds); 
... 
+0

先生它的工作完美的thnx建議和幫助:) – Atul