如何通过dateTimePicker过滤DataGridView [英] How to filter DataGridView by dateTimePicker

查看:61
本文介绍了如何通过dateTimePicker过滤DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家都有winstime形式的datetimepicker和datagridview,。我真的希望用户从datetimepicker中选择日期,过滤的数据可以逐行显示在datagridview中吗?我的主要目的是希望用户选择日期并返回该日期的数据。但问题是我的应用程序正在访问已构建的Access数据库,并由另一个应用程序使用,日期字段为此格式

Hi all have a datetimepicker and a datagridview in wins form, .Ideally I want a 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.But issue is my app is accessing an Access DB that was built and is used by another app and the date fields is in this Format

04/03/2015 11:23(dd/MM/yyyy mm:ss



而DateTimePicker是MM / DD / YYYY我可以做些什么来实现我的目标,下面是我在dateTimePicker方法中使用的代码来过滤我的数据,任何建议?


whereas DateTimePicker is MM/DD/YYYY what can I do to achiever my goal ,below is the code am using in dateTimePicker Method to Filter my data, any suggestions?

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 MYTABLE WHERE DateColumn = '{0}'", dt);
    using (var adap = new OleDbDataAdapter(strSql, connection))
    {
        DataTable table = new DataTable();
        adap.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
}

推荐答案

看一下格式化日期字符串的不同方法

自定义日期和时间格式字符串 [ ^ ]



添加时间信息

Have a look at the different ways to format a date string
Custom Date and Time Format Strings[^]

Added time information
string strSql = String.Format("SELECT * FROM MYTABLE WHERE DateColumn = '{0}'", dt.ToString("MM/dd/yyyy HH:mm"));





HH将以24小时格式为您提供价值。

......会给你12小时格式的值。



HH will give you values in 24 hour format.
hh will give you values in 12 hour format.


这篇关于如何通过dateTimePicker过滤DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆