开发者

C# - Data filter by datetimepicker

开发者 https://www.devze.com 2023-04-01 19:17 出处:网络
I am newbie to C# and would like to give a try on it. May I kno开发者_运维技巧w how can I return the desire value from Database(MS Access 2007) filter by the datetimepicker?

I am newbie to C# and would like to give a try on it.

May I kno开发者_运维技巧w how can I return the desire value from Database(MS Access 2007) filter by the datetimepicker?

For example:

I have a datetimepicker and a datagridview in form1, . User to select date from datetimepicker and filtered data can show in datagridview row by row?

My main purpose would like the user to pick up a date and return data fall in that date.

Many thanks in advance! Gary Yee


Given that you have column of date/datetime in your access database, you can filter data using WHERE clause in your SQL statement for example, inside ValueChanged event of DateTimePicker control

DateTime dt = datetimepicker.Value;

//Connection string to connect to access database
string strConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;";
using (var connection = new OleDbConnection(strConn)) {
   string strSql = String.Format("SELECT * FROM YOURTABLE WHERE DateCol = '{0}'", dt);
   using (var adap = new OleDbDataAdapter(strSql, connection)) {
      DataTable table = new DataTable();
      adap.Fill(table);
      GridView1.DataSource = table;
      GridView1.DataBind();
   }
]
0

精彩评论

暂无评论...
验证码 换一张
取 消