如何根据 TimeSpan 值过滤 DataColumn?

发布于 2024-09-24 03:09:20 字数 547 浏览 0 评论 0原文

使用 C#.NET 进行编程。我有一个DataTable,其中有一列名为Time,其类型为System.TimeSpan。当我在 DataGridView 中显示 DataTable 时,我想过滤掉所有未列出时间的条目。我已尝试以下方法但无济于事。你能帮忙吗?

DataView myDataView = new DataView(SomeDataTable);
myDataView.RowFilter += "Time >= 1";  // doesn't work
myDataView.RowFilter += "Time >= #00:00:01#";  // doesn't work
myDataView.RowFilter += "Time <> ''";  // doesn't work
myDataView.RowFilter += "Time <> ''";  // doesn't work

Programming in C#.NET. I have a DataTable with a column named Time which is of type System.TimeSpan. When I display the DataTable in a DataGridView I want to filter out all the entries that don't have a time listed. I have tried the following but to no avail. Can you help?

DataView myDataView = new DataView(SomeDataTable);
myDataView.RowFilter += "Time >= 1";  // doesn't work
myDataView.RowFilter += "Time >= #00:00:01#";  // doesn't work
myDataView.RowFilter += "Time <> ''";  // doesn't work
myDataView.RowFilter += "Time <> ''";  // doesn't work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

灯角 2024-10-01 03:09:20

我找到了...

myDataView.RowFilter = "Convert(Time, System.String) <> ''";

I found it...

myDataView.RowFilter = "Convert(Time, System.String) <> ''";
蓝色星空 2024-10-01 03:09:20

如果有人遇到这个恼人的问题,不幸的是没有好的解决方案。
看来使用 System.TimeSpan 过滤 DataTable 不起作用。
我使用的解决方案是将 SQL 中的列类型从 Time(0) (或其他)更改为 NVARCHAR(8) ,然后您可以应用逻辑 :

StringBuilder builder = new();

// IsNull() is my own string extension
if (!ExecutionTimeFrom.IsNull())
{
    builder.Append($"AND TimeStamp >= '{ExecutionTimeFrom}' ");
}
if (!ExecutionTimeTo.IsNull())
{
    builder.Append($"AND TimeStamp <= '{ExecutionTimeTo}' ");
}

10:32:15) 转换回 System.TimeSpan 就这么简单

bool isTimeSpan = TimeSpan.TryParse(TimeFromSql, out TimeSpan time);

然后将 sql 表中的字符串从 hh:mm:ss ( 这两天我一直在想这个问题,希望能有所帮助:)

If someone bump into this annoying problem, unfortunately there is no good solve.
It seams that filter DataTable with System.TimeSpan is not working.
The solution I use is to change the type of the column in the SQL to NVARCHAR(8) from Time(0) (or whatever) and then you can just apply logic upon it like so:

StringBuilder builder = new();

// IsNull() is my own string extension
if (!ExecutionTimeFrom.IsNull())
{
    builder.Append(
quot;AND TimeStamp >= '{ExecutionTimeFrom}' ");
}
if (!ExecutionTimeTo.IsNull())
{
    builder.Append(
quot;AND TimeStamp <= '{ExecutionTimeTo}' ");
}

Afterward convert string from the sql table from hh:mm:ss (10:32:15) back into System.TimeSpan is as easy as:

bool isTimeSpan = TimeSpan.TryParse(TimeFromSql, out TimeSpan time);

I break my head on this for two days, I hope it will help :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文