使用DateTimePicker搜索DataGridView的记录? [英] Search DataGridView's Record using DateTimePicker?

查看:69
本文介绍了使用DateTimePicker搜索DataGridView的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用DateTimePicker
搜索保存在DataGridView 中的记录
我的DataGrid名称为"EventDate"上有一个Column ,并且已使用DateTimePicker向该Column输入新记录.

现在我要做的是我的DataGridView 将仅查看记录,具体取决于我的DateTimePicker上的值..

How am I going to search the records saved on my DataGridView using DateTimePicker

I have a Column on my DataGrid name "EventDate" and I have used the DateTimePicker to input new records to that Column.

Now I want to do is my DataGridView will only view the records depends on the value on my DateTimePicker..

推荐答案

如果您的dataGridView1 已绑定到DataSource bindingSource1, ,然后在DateTimePicker 控件的ValueChanged 事件中使用以下代码,过滤BindingSource, ,以仅显示在DateTimePicker中具有选定日期的记录.
If your dataGridView1 is bound to DataSource through bindingSource1, then use the following code in ValueChanged event of DateTimePicker control, to filter the BindingSource, to show only those records which have the date selected in the DateTimePicker.
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
        bindingSource1.Filter = string.Format("EventDate = #{0}#", dateTimePicker1.Value.ToLongDateString());
}


如果您以文本格式将日期插入数据库,例如使用DateTimePickerToShortDateString() ,则在bindingSource1的过滤器字符串中使用相同的文本格式,使用''代替#,如下所示


If you have inserted the date into database in text format, say using ToShortDateString() of DateTimePicker, then use the same text format in the filter string of bindingSource1 using '' instead of #, as below

private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
    bindingSource1.Filter = string.Format("EventDate = '{0}'", dateTimePicker1.Value.ToShortDateString());
}


这篇关于使用DateTimePicker搜索DataGridView的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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