views:

15

answers:

2

I am working on a Repeater that reads from a table with a layout something like:

string Title
string Location
bool Water
bool Sewer
bool Picnic_Table
bool On_Beach
...

I am creating a list of amenities for each "Title" so I need to loop through the columns and get a list of amenities for each Title (or site). Optimally, I have a loop to go through the list. Something like

for each column
   if column is not Title or Location
      Append to StringBuilder "column name"

How do I get that column name to do the comparisons?

A: 

Hey,

Repeater is not a table-based control, so that's going to be hard from the server-side. What you can do is programmably create a table, or use the Grid control. Or, try to make the table a server-side control, and see if that works well with the repeater (I don't think it will). Or, if you can say ignore the first two columns, make the table row within the repeater item a server-side row as in , and use FindControl to find the item and do something with it. You would have to work with the cell index though, to show/hide cells within the column.

HTH.

Brian
+1  A: 

Here's how to get the column name in the OnItemCreated event of the Repeater control:

protected void rptOnItemCreated_OnItemCreated(object sender, RepeaterItemEventArgs e)
{
    string columnName = string.Empty;
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView drv = (DataRowView)e.Item.DataItem;
        DataRow dr = drv.Row;
        foreach (DataColumn dc in dr.Table.Columns)
        {
            // Do what you want with the column name
            columnName = dc.ColumnName;
        }
    }
}
DaveB
Exactly what I was looking for! Thanks!
Mike Wills