ASP.net programmatically bind dataset to gridview
So you are looking to create columns at runtime? Try this:
http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx
Alternatively, you can configure your gridview ahead of time in the aspx:
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="Product" SortExpression="ProductName" />
<asp:BoundField DataField="CategoryName" HeaderText="Category" ReadOnly="True" SortExpression="CategoryName" />
<asp:BoundField DataField="UnitPrice" DataFormatString="{0:c}" HeaderText="Price" HtmlEncode="False" SortExpression="UnitPrice" />
</Columns>
And make sure you set AutoGenerateColumns to false.
The GridView by default will automatically generate all of the columns based on the data being bound.
Set GridView.AutoGenerateColumns = false
and then define the columns you want through the GridView.Columns
Property.
Two ways I can see to do this, depending on what you can change and what has to stay the way it is:
1) If you have control of the query/proc that generates the dataset, and can do so without adversely affecting other grids/pages, modify the query so that it only SELECTs the columns you use. You can still use non-selected columns in JOIN and WHERE, and you get the added bonus of not having to add every column to ORDER BY.
2) If you only have control of the grid and the codebehind that's used to generate it, you can set as a DataGrid parameter, the AutoGenerateColumns = false, and then nest within your DataGrid, the BoundFields you need to show. This also allows you the power to format, make column names, etc.
If you can do both of these things, your Grid should load faster, as well.