Dave Burke - Freelance .NET Developer specializing in Online Communities

A freelance .NET Developer

Dataview-to-Datatable: A CopyTo to call my own

I wanted to copy a dataview to a datatable this afternoon and it proved an interesting case.  The data consisted of file and folder info, and I needed to group by folder name with an onitembound call to determine the count of files by folder.  The dataview was sorted, filtered, and ready to go, yet I couldn't make the leap.

A dotnet newsgroup post to "Neil" read:

Neil,

You could use something like that

1. Use RowFilter property of your DataView to filter your rows.
2. Copy all rows to array using CopyTo method of DataView.
3. Use ImportRow or LoadDataRow method of DataTable to load rows from array into DataTable

--
Val Mazur
Microsoft MVP

(Thanks Val!)  Which made great sense to me, but I kept getting datarow to datarowview mistype errors.  So I wanted to know more about this DataView CopyTo method.  The VS.NET help page was one of those lamers you occasionally encounter at

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/
frlrfSystemDataDataViewClassCopyToTopic.htm

And further Googling made me think that it was a phantom method that no one really uses.  So I ended up with the following.  The cool thing was that I discovered the datatable.select() method.  Didn't know about that at all!

Comments

Darren Neimke said:

Dave, this is what I came up with last week:

http://weblogs.asp.net/dneimke/posts/9689.aspx
# July 9, 2003 8:44 AM

Daniel said:

Hi,

it is better and fast to use only this:

DataView dvView = dtTabla.DefaultView;

dvView .RowFilter = "filter";

DataTable dtFiltered = dvView.ToTable();

that is!

# September 21, 2007 11:57 AM

sany said:

I was searching this for hour n after that i got this m so happy

Thanks

# August 1, 2008 3:36 AM

sudhakar said:

Thanks for the help.. This is really usefull.

# November 4, 2008 3:44 AM

Bernie Fahlde said:

Thanks!

I hope this would be the fastest method also ?!.

I hope so.

# January 18, 2009 5:06 PM

Sachin said:

Thanks

That is Awesome :)

# August 20, 2009 1:43 AM

Manoj said:

Really worked, Thanks and Great thing to learn today.

# October 10, 2009 7:28 AM

belveder said:

why dont you use a method named dataview.toTable()

this return a datatable

# December 10, 2009 11:58 AM

darwin said:

thanks a lot....

DC

# April 7, 2010 2:14 PM

Iqbal said:

It helps to slove my problem, Thanks

# June 29, 2010 10:12 AM

etaxmentor said:

This Code is working Fine .. thanks for your post...

# July 9, 2010 9:00 AM

Paradi$e said:

belveder spot on, perfect quick solution! Thanks so much.

Gridview -- DataView---DataTable.

DataView dvView = (DataView)GridView1.DataSource;

dvView.ToTable();

# April 11, 2011 6:43 PM

Tripti said:

How to do this in ASP.Net 1.1? Please help.

# April 21, 2011 8:23 AM

santhosh said:

thanks for your answer..

# January 10, 2012 1:08 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)