Search code examples
asp.netrepeater

how to get 5x5 grid in repeater control


I want to use a repeater as a 5x5 grid. I have 25 records and I want to display them in 5 rows and 5 columns.


Solution

  • I agree with @brian chandley that the DataList control is an easy to use control. If you're prefer to use a casual table though, you could try the following - in the xml side:

    <asp:Table ID="myTable" runat="server">
    
    </asp:Table>
    

    In some event in the code behind (presumably load or some click event)

        List<string> myEmployerList = yourFunctionToPopulateList();
    
        TableRow row = null;
        for(int j=0;j<myEmployerList.Count;j++)
        {
            if (j % 5 == 0) row = new TableRow();
            TableCell cell = new TableCell();
            cell.Text = myEmployerList[j];
            row.Cells.Add(cell);
            if (j % 5 == 4) myTable.Rows.Add(row);
        }
    
        if(j % 5 != 4) myTable.Rows.Add(row); // Catch uneven rows at the end