i have an datatable with 12 columns.now i need to remove all the columns except at the position "0"
i can remove individually by specifying the columns name. but i dnt want to do that.as it is not best way to code.
is there any other i can do that
thanks
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.
I would suggest you to create a new List<T> where T will be DatColumn. Then loop through the columns of DataTable, and add the columns you want to remove into the generic list.
Go backwards through the columns and remove each one. You have to go backwards to avoid an index out of range exception.
// index should include zero
for( int index=table.Columns.Count-1; index>=0; index-- )
{
table.Columns.RemoveAt( index );
}
VB.Net Lovers:
'index should include zero
For index As Integer = table.Columns.Count - 1 To 0 Step -1
table.Columns.RemoveAt(index)
Next
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With