DataGridView RowFilter按日期 [英] DataGridView RowFilter By Date

查看:633
本文介绍了DataGridView RowFilter按日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 dataGridView_flaggedComments 有10行。 Comment_Date 列中的值显示日期格式为 31/12/2014 12:01 PM ,如果我从 comboBox_stockDates (例如31/12/2014)中选择一个日期(没有时间部分),我希望它过滤(并显示)所有具有 31/12/2014 xx:xx xx

My dataGridView_flaggedComments has, let say, 10 rows. Value in the Comments_Date column's cell shows dates in the format of 31/12/2014 12:01 PM, if I choose a date (which is without time portion) from comboBox_stockDates (e.g. 31/12/2014), I want it to filter (and display) all the rows that have 31/12/2014 xx:xx xx.

以下代码即使在选定的日期(例如31/12/2014)匹配包含 31/12/2014 xx:xx xx 的行。任何想法这里出了什么问题?

The below code would yield zero result even when the selected date (e.g. 31/12/2014) matches the rows which contain 31/12/2014 xx:xx xx. Any idea what has gone wrong here?

string dtFilter = string.Format("Comments_Date = #{0}#", comboBox_stockDates.SelectedItem.ToString());
(dataGridView_flaggedComments.DataSource as DataTable).DefaultView.RowFilter = dtFilter;

或者,有没有办法将这两个日期转换成字符串,然后比较?我试图使用LIKE操作符,但错误说不能用来比较DateTime和String。

Alternatively, is there a way to convert both dates to string then compare? I tried to use LIKE operator but error says cannot be used to compare between DateTime and String.

任何帮助和指导都将非常感谢!谢谢。 :)

Any help and guidance would be much appreciated! Thank you. :)

推荐答案

我已经修复了,所以我提出自己的答案。这对未来可能有帮助的人正在寻找类似的问题。

I've got this fixed, so I propose my own answer. It might be of helpful in the future for someone who's looking for similar question.

string str = comboBox_stockDates.SelectedItem.ToString();
DateTime date = DateTime.ParseExact(str, "dd/MM/yyyy", CultureInfo.GetCultureInfo("en-GB"));
string dtFilter = string.Format(
    "[Comments_Date] >= '{0} 12:00:00 AM' AND [Comments_Date] <= '{0} 11:59:59 PM'", date.ToString("dd/MM/yyyy"));
(dataGridView_flaggedComments.DataSource as DataTable).DefaultView.RowFilter = dtFilter;

这篇关于DataGridView RowFilter按日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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