Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 ?
Posted
Updated 22-Mar-23 4:26am

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:
C#
        private DataView dvBooks = new DataView();
...
            DataTable dt = myList.ToDataTable();
            dvBooks = new DataView(dt);
            dgvBooks.DataSource = dvBooks;

Then use the RowFilter property to filter the displayed rows:
C#
dgvBooks.RowFilter = "[date] LIKE '%-" + monthNumber.ToString("D2") + "T%'";
The system will only show matching records.
 
Share this answer
 
Comments
Gee Varghese 27-Jul-14 11:13am    
I don't use Gridviews.
I have loaded my Datatable with data from a List<class objects="">.
My requirement is to load data from List<Objects> to the Datatable which have date of this month. Or show data of this month from Datatable. I prefer to choose Linq.
OriginalGriff 27-Jul-14 11:28am    
So why not just use Linq on the List? That's trivial, isn't it?
Then load the DT from that.
Gee Varghese 27-Jul-14 11:38am    
How do we do that ?
OriginalGriff 27-Jul-14 11:57am    
Which? Linq on the List or load the table?
Gee Varghese 27-Jul-14 11:58am    
Whichever, either on the list or the Datatable.
ADO.NET(DataTable.Select):

DataRow[] filteredRows = tb.Select("CREATOR LIKE '%" + searchstring + "%'");


ADO.NET(DataView.RowFilter):

 tb.DefaultView.RowFilter = "CREATOR LIKE '%" + searchstring + "%'";
 
Share this answer
 
v2
LINQ-To-DataSet (which i prefer):

var filtered = tb.AsEnumerable()
    .Where(r => r.Field<String>("CREATOR").Contains(searchstring));
ADO.NET(DataTable.Select):

DataRow[] filteredRows = tb.Select("CREATOR LIKE '%" + searchstring + "%'");
ADO.NET(DataView.RowFilter):

 tb.DefaultView.RowFilter = "CREATOR LIKE '%" + searchstring + "%'";
 
Share this answer
 
Comments
CHill60 22-Mar-23 11:30am    
Please don't post multiple solutions to the same question - it is confusing. You can use the "Improve Solution" link to update your first solution post.
As an aside, I notice that all the solutions you are posting are to old questions that already have solutions. Be aware that some members will view this as "rep point hunting" and may start the process of disabling your account. It is better to stick to new posts where the OP still needs help
Dave Kreskowiak 22-Mar-23 13:50pm    
And start explaining your answers. Just dumping code into an answer doesn't cut it.
use Select method for Datatable to filter the record. This will return the array of DataRow.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900