DataTable subset

C

ciccone1978

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!
 
G

Guest

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;
 
C

Chris Shepherd

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top