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