DataView.ToTable method in the 2.0 Framework

Published Tue, Jun 6 2006 0:37 | William

This is a really simple trick but it's way cool.  Now that I have some time again, I've been trying to get up to speed with LINQ and ADO.NET 2.0.  God I missed it. Anyway, this comes up in the newsgroups all the time so I figured I'd write up a quick sample to point to. 

Problem:  I want to create a new datatable with only a subset of the columns of the original datatable.

Solution:  Thanks to the ADO.NET team, it's cake:

class Program
{
private static Actresses DemoData = new Actresses();

static void Main(string[] args)
{
LoadData();
DataTable dt = DemoData.Stars.DefaultView.ToTable("Stars", true, new String[]{"FirstName", "LastName"});
foreach(DataRow dro in dt.Rows){
foreach(DataColumn dc in dt.Columns){
Console.WriteLine(dro[dc].ToString());
}
}
Console.ReadLine();
}

static void LoadData(){
Actresses.StarsRow dro = DemoData.Stars.NewStarsRow();
dro.FirstName = "Jenna";
dro.LastName = "Jameson";
dro.LovesBill = true;
DemoData.Stars.AddStarsRow(dro);
dro = DemoData.Stars.NewStarsRow();
dro.FirstName = "Tera";
dro.LastName = "Patrick";
dro.LovesBill = true;
DemoData.Stars.AddStarsRow(dro);
dro = DemoData.Stars.NewStarsRow();
dro.FirstName = "Aishwarya";
dro.LastName = "Rai";
dro.LovesBill = true;
DemoData.Stars.AddStarsRow(dro);

}
}
All that you need to do is create a view or use the default view, then call its ToTable method passing in a table name and a String array of the column names you want to populate in the new table.  There are four overloads and since i love dataviews, I'm going to flesh out a few in depth articles doing some much less basic stuff. (Spoiler - There's a Distinct argument).

Filed under:

Comments

# Brian Madsen said on June 8, 2006 6:01 AM:

yeah ADO.Net 2 has a lot of nice features...the ToTable() method is one of my favorites together with the TableAdapter...

keep it coming Bill!! always a good read!

# William said on June 8, 2006 8:30 PM:

Bri - the ability to build a dataview with restricted columns is pretty awesome - I have a ton of helper classes to do stuff like this, or select distinct, so having it built in is nice b/c  it's 'out of the box'.  Now I have to install all of those libraries in the GAC on each box we develop on and it's a pain.

As far as TableAdapters - I haven't been all that impressed with them but admittedly I haven't had much experience with them yet.  So like a lot of stuff that I'm not impressed with at first, it's probably b/c of ignorance (although I'm embarassed to say it, I wans't all that impressed with LINQ at first either - talk about doing a 180 degree turn on something).

What sort of scenarios are you using TableAdapters for?

# Brian Madsen said on June 8, 2006 10:17 PM:

Hey Bill...i'm using TA's primarily for restricted data linked to an SP which calls data that a user on our network might not necessarily have access to normally.

that way the data access tier is produced for him/her without any interactions/requirements from them..allowing me to secure our data without exposing scenarios to new (or untrusted) users - all without having to give them access to the tables within sql server..

Search

This Blog

Tags

Community

Archives

News

My other sites

Cool Stuff

Book Stuff

Security

ORM

Data Access

Funny Stuff

Compact Framework Stuff

Web Casts

My KnowledgeBase Articles

My MVP Profile

Design Patterns

Performance

Debugging

Remoting

My Fellow Authors

My Books

LINQ

Misc

Speech

Syndication

Email Notifications