Thursday, October 25, 2012

How can I reorder columns in a data table?

Problem: How can I reorder columns in a data table?

 Solution: Columns in datatable can be reordered using SetOrdinal method of datatable.
Here is an example code:


DataTable dt = new DataTable();
dt = lst.GetDataTable();
dt.Columns["bank"].SetOrdinal(6);
dt.Columns["remarks"].SetOrdinal(dt.Columns.Count - 1);

Here "bank" column will be set at position 6 and "remarks" column at last of all columns in the table.

Hope this small tips will be helpful.


1 comment: