1

I have One DataTable may have more Columns. But "NetAmount", "TotalAmount", "Destination" are the DataTable Columns which always present in the DataTable.

Here I want to Remove the three Columns such as "NetAmount", "TotalAmount" and "Destination" from the DataTable and to take the other column values in the DataTable.

I tried like the below and get the Desired Output.

  dtAttribute.Columns.Remove("NetAmount"); //dtAttribute is the Main DataTable
  dtAttribute.Columns.Remove("TotalAmount");
  dtAttribute.Columns.Remove("Destination");
  DataTable dtItem = dtAttribute.Copy();      

But it looks like very childish and lengthy. Is there any other method to do? Please give suggestions.

4
  • Are you sure this works? According to the documentation DataColumnCollection.Remove is a void method, i.e., it should not return a value. Please show us your real code instead of something that looks like your code. Commented Jul 21, 2011 at 12:17
  • Are you sure you took that sample from the code that compiled ok? If I am not wrong, the Remove method is of type void. Also calling Remove 3 times is not lengthy / childish if that's the only method provided by the API. Commented Jul 21, 2011 at 12:18
  • @Heinzi & Vijay: hi sorry for both. I just typed without actually gothrough my code. Now I edited my Question. Please have a look at it. Commented Jul 21, 2011 at 12:23
  • @thevan: Why do you make a copy after removing the columns? Commented Jul 21, 2011 at 12:25

3 Answers 3

7

There's nothing wrong with your code (except that you are copying the table after removing the columns -- are you sure that this is what you want?).

If you want something more abstract (instead of repeating the same line again and again), you might consider removing the columns in a loop:

var dtItem = dtAttribute.Copy();    // if you want to keep a copy of the original table

var toRemove = new string[] {"NetAmount", "TotalAmount", "Destination"};
foreach (col in toRemove)
    dtItem.Columns.Remove(col);
Sign up to request clarification or add additional context in comments.

6 Comments

What if I wanted to remove let's say column 0 to 9?
@SearchForKnowledge: Well, DataTable.Columns returns a value of type DataColumnCollection, and DataColumnCollection has a method called RemoveAt. Does that help you answer your question on your own?
@Heinzi I tried that so let's say I have four columns, 0, 1, 2, 3. Every time I iterate to use RemoveAt() it removes every other column but thank you :) +1
@SearchForKnowledge: As soon as you remove row 0, the (old) rows 1, 2, 3 become (new) rows 0, 1, 2. There are two obvious ways to fix this. Can you guess what they are?
@SearchForKnowledge: Exactly! The other option is to remove 3, 2, 1, 0 instead of 0, 1, 2, 3.
|
0

Instead of removing the columns, how about not putting them in the DataTable in the first place?

3 Comments

I am getting the DataTable from the BackEnd. And I also need those columns for one another purpose.
I have a DataSet, how can I add the first half of it into a DataTable.
@SteveWellens I use DataTable with ExcelPackage (EPPlus)
0

First select columns which you want to remove, then remove them

List<string> toRemove = dt.Columns.Cast<DataColumn>().Where(c => c.ColumnName.StartsWith("ExtraColumn")).Select(c => c.ColumnName).ToList();
foreach (var col in toRemove) dt.Columns.Remove(col);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.