Script(s)

what I learn is what u c

Asp.Net Filter Datatable by Column

with 6 comments

Filter Datatable by Rows

If you need to get subset of a big dataset into datatable Rowwise …


Dim dtEmp as DataTable
Dim dsEmployee as New DataSet

dsEmployee = someobject.getdata()  'dataset can be populated in many ways which is not explained here.

Dim sExpr as String
Dim drRows() as DataRow, drSingleRow as DataRow

sExpr = "EmpID > 100"  'Condition

drRows = dsEmployee.Tables(0).Select(sExpr)

'If you need to add Sort Order it can be added to sExpr

Dim sSortOrder as String
sSortOrder = "EmpName DESC"
drRows = dsEmployee.Tables(0).Select(sExpr,sSortOrder)

For Each drSingleRow in drRows
 dtEmp.ImportRow(drRows)
Next

' Datatable dtEmp has filtered records

Filter Datatable by Column

If you need to get subset of big dataset into datatable columnwise…


Dim dtEmp as DataTable
Dim dsEmployee as New DataSet

dsEmployoee = someobject.getdata()  'dataset can be populated in many ways which is not explained here.

'This copies the structure and data

dtEmp = dsEmployee.Tables(0).Copy

dtEmp.Columns.Remove("Unwanted Column 1")
dtEmp.Columns.Remove("Unwanted Column 2")
dtEmp.Columns.Remove("Unwanted Column 3")
Advertisements

Written by gchandra

January 7, 2008 at 4:13 pm

6 Responses

Subscribe to comments with RSS.

  1. pls let us know how to read a feed into a datatable in asp.net

    swarna

    January 13, 2008 at 7:57 am

  2. This article explains it very nicely. Please take a look.

    http://aspnet.4guysfromrolla.com/articles/031903-1.aspx

    gchandra

    January 13, 2008 at 9:27 am

  3. thanks a lot dude

    ace

    December 19, 2008 at 3:56 am

  4. Line 21 in the first example, shouldn’t it be passing drSingleRow to ImportRow?

    Once dtEmp has filtered records how do I reference the columns? Will it have the same schema as the row which was imported? I’m having problem with this, cannot reference the filtered data table columns.

    Kashif

    March 16, 2009 at 9:25 am

  5. Kashif,

    Clone the original datatable before inserting via ImportRow, this way you can access the column names.

    dtEmp = dsEmployee.Tables(0).Clone

    Or

    Use column numbers (0), (1)

    Ganesh

    March 16, 2009 at 9:31 am

  6. Thanks, friends.

    Oskar Merchan

    March 25, 2009 at 3:45 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: