I am declaring a SQL string that looks something similar to the following:
string SQL = "SELECT Column1, Column2, Column3, Date1, Date2 FROM Somewhere";
The number of columns can vary by circumstance and the dates can be called different names such as StartDate, InterestDate and so on.
What I would like to do is bind this to an ASP.NET Repeater and create a table as below for the example above:
<table>
<tr>
<th>Column1</th>
<th>Column2</th>
<th>Column3</th>
<th>Date1</th>
<th>Date2</th>
</tr>
<tr>
Rows of values...
</tr>
</table>
I am fine with binding the data to a Repeater and using Eval but that is dependent on a fixed number of columns and column names. Can this be done in a programmatic way?
If you use a GridView you can set the AutoGenerate columns property to true (it is true by default) and then bind the GridView to your data.
To make is super simple, you may want to use a SQLDataSource control.
The asp.net site has an overview of databinding in asp.net (uses the ObjectDataSource, but the concepts is exactly the same). This page also covers some of the ways to customize the GridView.
Update: Here is a sample of how you could handle the rowdatabound event to format the date columns (per comments on other answer). This could probably be done better, but this should get you started.