Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

DataTable subset

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ciccone1978@gmail.com - 27 Aug 2007 10:30 GMT
Hi everybody,
Supposing I have a DataTable with the following columns ["A", "B",
"C", "D"].
I need to copy that DataTable with all its data but filtered by
columns, that is I want
to have a new DataTable with all rows and the columns ["A", "C"].

What can I do?

Bye!
Manish Bafna - 27 Aug 2007 12:24 GMT
Hi,
you can try code something like this below:
DataTable dt = new DataTable();
            DataColumn col1 = new DataColumn();
            DataColumn col2 = new DataColumn();
            col1.ColumnName = "Name";
            col2.ColumnName = "Age";

            col1.DataType = typeof(System.String);
            col2.DataType = typeof(System.Int32);
            col2.ColumnMapping = MappingType.Hidden;
            dt.Columns.Add(col1);
            dt.Columns.Add(col2);

            DataRow row1 = dt.NewRow();

            row1["Name"] = "manih bafna";

            dt.Rows.Add(row1);
            dataGrid1.DataSource = dt;
Signature

Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

> Hi everybody,
> Supposing I have a DataTable with the following columns ["A", "B",
[quoted text clipped - 6 lines]
>
> Bye!
Chris Shepherd - 27 Aug 2007 13:44 GMT
> Hi everybody,
> Supposing I have a DataTable with the following columns ["A", "B",
[quoted text clipped - 4 lines]
>
> What can I do?

You could copy the DataTable in its entirety, and then remove the
columns you don't need.
For example:

DataTable newDT = oldDT.Copy();
newDT.Columns.Remove("B");
newDT.Columns.Remove("D");

someObj.someMethod(newDT);

Chris.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.