如何搜索C#DataTable [英] How to search C# DataTable

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

问题描述

我有一个DataTable,它有两列,id和date(以DateTime格式 - > yyyy-dd-MMThh:mm:ss)。

我需要显示数据对应于这个月。怎么可能?

I have a DataTable which has two columns, id and date (which is in DateTime format -> yyyy-dd-MMThh:mm:ss).
I have a requirement of showing data that corresponding to this month. How is it possible ?

推荐答案

一般来说,我不搜索DataTables:我倾向于过滤DataViews。

假设你正在使用一个DataGridView,使用DataTable创建一个类级DataView,并使用DataView作为DGV的DataSource:

Generally, I don't search DataTables: I tend to filter DataViews instead.
Assuming you are using a DataGridView, create a class level DataView using the DataTable as it's source and use the DataView as the DataSource of the DGV:
        private DataView dvBooks = new DataView();
...
            DataTable dt = myList.ToDataTable();
            dvBooks = new DataView(dt);
            dgvBooks.DataSource = dvBooks;



然后使用RowFilter属性过滤显示的行:


Then use the RowFilter property to filter the displayed rows:

dgvBooks.RowFilter = "[date] LIKE '%-" + monthNumber.ToString("D2") + "T%'";

系统只显示匹配的记录。

The system will only show matching records.


使用选择方法对Datatable过滤记录。这将返回DataRow数组。
use Select method for Datatable to filter the record. This will return the array of DataRow.


这篇关于如何搜索C#DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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