Skip to content Skip to sidebar Skip to footer

How To Make A Comparison Html Table Using Asp Repeater (data Rotation)

I need to make a comparison table in HTML format, the problem is that the items in database comes as a columns but in comparison table it must be a rows! Example The data in datab

Solution 1:

A possible solution:

Given the following DataTable as datasource:

protected DataTable Data
{
    get
    {
        if (ViewState["Data"] == null)
        { 
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Name");
            table.Columns.Add("Color");
            table.Columns.Add("Weight", typeof(int));

            table.Rows.Add(1, "Ball", "Red", 10);
            table.Rows.Add(2, "Table", "Black", 50);
            table.Rows.Add(3, "Chair", "Green", 30);
            ViewState["Data"] = table;
        }
        return (DataTable)ViewState["Data"];
    }
}

And some ASP code for looping and building the table:

<table>
<%
  for (int i = 0; i < Data.Columns.Count; i++)
  {
        %>
        <tr><td><%= Data.Columns[i].ColumnName %></td>
            <%
                for (int j = 0; j < Data.Rows.Count; j++)
                {
                    %>
                    <td><%= Data.Rows[j][i] %></td>
                    <%
                }

             %>
        </tr>
        <%  
  } 
%>
</table>

Solution 2:

So this isn't for ASP.NET but you might be able to convert it. This is code I use to create a dynamic pivot tables in SQL Server. This allows you to do what you want without having to know how many columns there are going to be. The thing about pivot tables is that they require a calculation so you will have to do that to get it to work. Hope this helps.

DECLARE@columnsvarchar(500)

Select@columns= STUFF(( selectdistinct'],['+cast(Name asvarchar)
              from table_name as t1
              orderby'],['+cast(Name asvarchar)
              for XML path('')
              ), 1, 2, '') +']'DECLARE@queryvarchar(max)

SET@query='select *
             from table_name
             pivot
             (
              count(Name)
              for Name in ('+@columns+')
             )as pvt'execute(@query)

Post a Comment for "How To Make A Comparison Html Table Using Asp Repeater (data Rotation)"