Reputation: 434
i have a database table
containing ID
and Name
.
in Default.aspx i only show the Name
cell (column) off the DB table, this is not a big deal.
But GridView generates an html table like this (of course i removed gridview header):
------------
| Name 1 |
------------
| Name 2 |
------------
| Name 3 |
------------
How can i let the above GridView:
1 - to spread the Names over 1 row
?
2 - and to skip to a new row
every 2 records
(every 2 names)?
<td>
, but unfortunately, as you can see, though it will skip every 2 names but it will show the same name (same record) twice.<asp:Repeater ID="repeater1" runat="server" DataSourceID="AccessDataSource1">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name")%>
</td>
<td>
<%# Eval("name")%>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<tr>
and </tr>
to HeaderTemplate and FooterTemplate, it now does show them all on 1 row but unfortunately it doesn't create a new row every 2 Names:<asp:Repeater ID="repeater1" runat="server" DataSourceID="AccessDataSource1">
<HeaderTemplate>
<table>
<tr>
</HeaderTemplate>
<ItemTemplate>
<td><%# Eval("name")%></td>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
Upvotes: 0
Views: 171
Reputation: 6249
If you are dead set on using a Repeater
, look at the first answer for this SO question:
How do you show x items per row in a repeater?
A much simpler approach would be using the DataList
control. It has built-in ability to break items across rows using these two properties:
DataList1.RepeatColumns = 2;
DataList1.RepeatDirection = RepeatDirection.Horizontal;
Upvotes: 1
Reputation: 1211
Assuming before you bind your data to the GridView you have your data in a DataTable or DataReader - You could loop through the datasource and contruct a new DataTable with 3 columns, which will hold names, then bind that to the grid.
Psuedo example for 3:
DataTable dt = new DataTable();
dt.columns.add("name1");
dt.columns.add("name2");
dt.columns.add("name3");
int pointer = 1;
DataRow dr = null;
for(int i=0;i<sourceTable;i++)
{
if( pointer==1 )
{
dr = dt.newRow();
dr["name1"] = sourceTable.Rows[i]["Name"];
dt.Rows.Add(dr);
pointer++;
}
else if( pointer==2)
{
dr["name2"] = sourceTable.Rows[i]["Name"];
pointer++;
}
else
{
dr["name3"] = sourceTable.Rows[i]["Name"];
pointer=1;
}
}
Upvotes: 1
Reputation: 334
Since you are only looking for 2, you can use the alternating item template.
<AlternatingItemTemplate>
<td>
<%# Eval("name")%>
</td>
</tr>
</AlternatingItemTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name")%>
</td>
</ItemTemplate>
You may have to swap the <tr> and the </tr> tags
Upvotes: 1