C#: Changing the order of columns when binding DataTable to a GridView
- by Nir
How is it possible to change the displayed order of columns from a DataTable?
For example, dataTable "dt" contains two columns "a" and "b". I bind it to a GridView like this:
gridView.DataSource = dt;
gridView.DataBind();
But I'd like the GridView to display "b" first (leftmost).
Important point: I'm using this to export to Excel and there's no actual output to screen, using:
HtmlTextWriter htw = new HtmlTextWriter(sw);
gridView.RenderControl(htw);
Thanks!