473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

DataTable subset

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!

Aug 27 '07 #1
2 2369
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;
--
Hope this helps.
Thanks and Regards.
Manish Bafna.
MCP and MCTS.

"ci*********@gmail.com" wrote:
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!

Aug 27 '07 #2
ci*********@gmail.com wrote:
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?
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.
Aug 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jensen bredal | last post by:
I have an array of datarow . Is there a performance efficient way of deriving a datatable oject containing only the rows in my array. ? I want to avoid uding foreach . Many thanks JB
4
by: Rich | last post by:
Hello, I need to store various values that I will need to look up later on. I have been using hashtables and arraylists. But I can only store 2 items per row in a hashtable - key, value, and...
6
by: Pete Wittig | last post by:
Hi, I have a DataTable and I want to get a subset of the rows within it. I use the Select method to get my subset and the results are in a DataRow. I want to put those Rows back into a...
2
by: =?Utf-8?B?Sm9iIExvdA==?= | last post by:
How can I reconcile changes between two data table and create a subset of those changes. I have two data tables with same schema which gets populated from two different xml files. I want to get...
1
by: byrd48 | last post by:
Hi, am attempting to create a DataTable by copying a table from a data set, then filter the rows as follows: DataTable dt = DataSet1.Tables.Copy; DataRow dr = dt.Select("myexpression"); ...
9
by: Nathan Sokalski | last post by:
I have a DataTable from which I only need a certain range of the DataRows. What I would like to do is copy a range of rows from one DataTable to a new DataTable like the following: For i As...
5
by: jehugaleahsa | last post by:
Hello: What is the point of using a DataTable in ASP .NET? We are unsure how you can use them without 1) rebuilding them every postback, or 2) taking up precious memory. We are not sure how to...
1
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
How would I copy all data from my DataSet that meets a filtering condition to a new DataTable? Is this what I want? DataTable table = new DataTable(); table =...
11
by: John Dann | last post by:
Is there a concise/efficient way to retrieve blocks of rows from a datatable with VB2005? I've got a datatable (let's call it AllData), constructed programmatically, that contains a lot of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.