使用DateTimePicker搜索DataGridView的记录? [英] Search DataGridView's Record using DateTimePicker?
问题描述
如何使用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 yourdataGridView1
is bound toDataSource
throughbindingSource1,
then use the following code inValueChanged
event ofDateTimePicker
control, to filter theBindingSource,
to show only those records which have the date selected in theDateTimePicker
.
private void dateTimePicker1_ValueChanged(object sender, EventArgs e) {
bindingSource1.Filter = string.Format("EventDate = #{0}#", dateTimePicker1.Value.ToLongDateString());
}
如果您以文本格式将日期插入数据库,例如使用DateTimePicker
的ToShortDateString()
,则在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屋!