Infolink

 

Search This Blog

Showing posts with label datatable. Show all posts
Showing posts with label datatable. Show all posts

Oct 21, 2012

How to Convert DataTable to Collection of Custom Class object

Imagine you have a DataTable with Records and you want to Convert that to a List of your Custom class objects ? how do you do that ?

The normal -straight forward way is to Loop through  each DataRow, Create a new instance of Custom class object, read the column values of the current row(iterator) in the loop and Set the Proerties of the object..

The below code shows looping the DataRows and Creating an object of our Custom Customer Class and Setting the Properties and Adding to the Collection of Customer objects.

We can avoid this handwritten ForEach loop by using some LINQ syntax.  Generally LINQ queries works on data sources which implement the IEnumerable<T>/ IQueryable<T> Interface. But DataTable does not
implement any of these. So we can not directly apply LINQ queries on a DataTable. 

But DataTable class has an extension method called AsEnumerable which returns an IEnumerable collection of DataRow. So we can apply the AsEnumerable function on a DataTable and then play with some LINQ on the resulting collection.

So we can replace our previous version of code with the below one which make use of LINQ

 



Oct 19, 2012

Gridview Paging using C#

A GridView is most often used to display the data retrieved from the database in Tabular form with features of Gridview like data paging, sorting and auto formats.

You can use C# code to bind the SQL data with GridView control and follow the following simple steps to make your ASP.Net GridView control with paging enabled.

First of all drag the GridView control from Data controls menu. It will add the GridView control HTML source code as given above. Now click on GridView control to load the control properties at right side panel.
Related Posts Plugin for WordPress, Blogger...