<p>I have a DataSet which I get a DataTable from that I am being passed back from a function call. It has 15-20 columns, however I only want 10 columns of the data.</p>

<p>Is there a way to remove those columns that I don't want, copy the DataTable to another that has only the columns defined that I want or is it just better to iterate the collection and just use the columns I need.</p>

<p>I need to write the values out to a fixed length data file.</p>

[{'answer_id': 75178, 'author': 'Tom Ritter', 'author_id': 8435, 'author_profile': 'https://Stackoverflow.com/users/8435', 'pm_score': 9, 'selected': True, 'text': '<p>Aside from limiting the columns selected to reduce bandwidth and memory:</p>\n\n<pre><code>DataTable t;\nt.Columns.Remove("columnName");\nt.Columns.RemoveAt(columnIndex);\n</code></pre>\n'}, {'answer_id': 75558, 'author': 'Timothy Carter', 'author_id': 4660, 'author_profile': 'https://Stackoverflow.com/users/4660', 'pm_score': 5, 'selected': False, 'text': '<p>To remove all columns after the one you want, below code should work. It will remove at index 10 (remember Columns are 0 based), until the Column count is 10 or less.</p>\n\n<pre><code>DataTable dt;\nint desiredSize = 10;\n\nwhile (dt.Columns.Count &gt; desiredSize)\n{\n   dt.Columns.RemoveAt(desiredSize);\n}\n</code></pre>\n'}, {'answer_id': 58115639, 'author': 'SU7', 'author_id': 8043435, 'author_profile': 'https://Stackoverflow.com/users/8043435', 'pm_score': 3, 'selected': False, 'text': '<p>The question has already been marked as answered, But I guess the question states that the person wants to remove multiple columns from a <code>DataTable</code>. </p>\n\n<p>So for that, here is what I did, when I came across the same problem.</p>\n\n<pre><code>string[] ColumnsToBeDeleted = { "col1", "col2", "col3", "col4" };\n\nforeach (string ColName in ColumnsToBeDeleted)\n{\n    if (dt.Columns.Contains(ColName))\n        dt.Columns.Remove(ColName);\n}\n</code></pre>\n'}, {'answer_id': 69431049, 'author': 'Hannington Mambo', 'author_id': 1909689, 'author_profile': 'https://Stackoverflow.com/users/1909689', 'pm_score': 0, 'selected': False, 'text': '<p>How about you just select the columns you want like this:</p>\n<pre><code>Dim Subjects As String = &quot;Math, English&quot;\nDim SubjectData As DataTable = Table.AsDataView.ToTable(True, Subjects.Split(&quot;,&quot;))\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/75123', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3208/']