Mahdi Hesari
Mahdi Hesari

Reputation: 337

Display column name and column value dynamically in desired style in Repeater ASP.NET control

All i need is to display data in a repeater or any other control like this:

<asp:Repeater ID="MyRepeater" runat="server" DataSourceID="MyDT">
    <ItemTemplate>
        <asp:Label Text="NAME OF FIRST COLUMN" runat="server" ID="NameLabel" />
        <br />
        <asp:Label Text="VALUE OF FIRST COLUMN" runat="server" ID="ValueLabel" />
        <hr />
    </ItemTemplate>
</asp:Repeater>

and then if i changed DataSourceID in code behind, repeater dynamically display new data in the same format. Why i need to do this? because there are many tables in my data base and i don't want to design a repeater for each table.

thank you very much.

Upvotes: 0

Views: 1607

Answers (1)

Symeon Breen
Symeon Breen

Reputation: 1541

You can get the column name in the OnItemCreated event of the Repeater.

e.g.

DataRowView rowview = (DataRowView)e.Item.DataItem;
DataRow row = rowview.Row;
foreach (DataColumn col in row.Table.Columns)
   {
      nameOfColumn = col.ColumnName;
      // then add that to the relevant control in the row
   }

If the number of columns is variable depending on table - then you will need to look at using nested repeaters and some code behind to control those.

Upvotes: 1

Related Questions