question

Kalpana-3650 avatar image
0 Votes"
Kalpana-3650 asked LanHuang-MSFT commented

Best control to use to populate a N number of tablse with rows of data in Asp.net dynamically

Hi

I have got a requirement where I have to generate a N number of tables based a list collection. In each of this tables, there would be a N number of rows.
The columns are fixed for the tables.
Which control is suitable for this purpose, I do not need any paging or sorting as the data would be sorted before it gets displayed.
I tried repeater control, but I am facing difficulty in creating the item template for it.
Are there any guidance that I can look up ?

Thank you.

dotnet-aspnet-webforms
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Best control to use to populate a N number of tablse with rows of data in Asp.net dynamically

A .NET DataSet set is a generic table collection.

I tried repeater control, but I am facing difficulty in creating the item template for it.

A GridView is very easy to populate.


0 Votes 0 ·
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered

Hi @Kalpana-3650,
I recommended that you use a repeater, it is used to display a repeated list of items.

I tried repeater control, but I am facing difficulty in creating the item template for it.

You said you encountered difficulties when using the repeater. Can you describe it in detail?
Other controls of ASP.NET data binding Web server, such as Gridview, you can refer to the following article:
https://docs.microsoft.com/en-us/previous-versions/aspnet/ms247243(v=vs.100)
I made a demo using a repeater, you can refer to it:
The code:
127532-test1.png

127498-test2.png
The Result:
127408-test.png


Best regards,
Lan Huang


If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.



test1.png (33.4 KiB)
test2.png (9.9 KiB)
test.png (12.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Kalpana-3650 avatar image
0 Votes"
Kalpana-3650 answered LanHuang-MSFT commented

HI Lan Huang

Yes, I am proceeding with a repeater control, the repeater control is created at the code behind, say if I have a collection of lists, for every list a method is run which creates the control and subsequently displays the data.
Here is the code : -

   List<List<Bottleneckv11.SelectAllPU_Result>> groupedList = pulist.ToList()
                    .GroupBy(u => u.ProcessTypeName)
                    .Select(grp => grp.ToList())
                    .ToList();
    
    
             for(int i = 0; i < groupedList.Count; i++)
             {
                 CreateDynamicRepeater(groupedList[i]);
             }

   internal void CreateDynamicRepeater(List<Bottleneckv11.SelectAllPU_Result> sublist)
         {
             Repeater rpt = new Repeater();
    
             ITemplate header = LoadTemplate("rpthdr.ascx");
             rpt.HeaderTemplate = header;
             //rpt.HeaderTemplate = header;
    
    
             TemplateBuilder itemTemplate = new TemplateBuilder();
    
             for (int i = 0; i< sublist.Count; i++)
             {
                    
                 itemTemplate.AppendLiteralString("<tr>");
                 itemTemplate.AppendLiteralString("<td>" + sublist[i].ProcessUnitID + "</td>");
                 itemTemplate.AppendLiteralString("<td>" + sublist[i].ProcessUnitName + "</td>");
                 itemTemplate.AppendLiteralString("<td>" + sublist[i].JobOrderName + "</td>");
                 itemTemplate.AppendLiteralString("<td>" + i + "</td>");
                 itemTemplate.AppendLiteralString("<td>" + sublist[i].Result + "</td>");
                 itemTemplate.AppendLiteralString("<td>" + sublist[i].TotalAvailable + "</td>");
                 itemTemplate.AppendLiteralString("</tr>");
                    
             }
    
             rpt.ItemTemplate = itemTemplate;
    
    
             TemplateBuilder footer = new TemplateBuilder();
             footer.AppendLiteralString("<tr><td>End of Table</td></tr></table>");
             rpt.FooterTemplate = footer;
    
            
             rpt.DataSource = sublist;
             rpt.DataBind();
             this.Controls.Add(rpt);
        

         }

Right now, the data is displayed but it is repeated and this is how it looks now. I do not want the data to be repeated. It gets repeated based on the count of list. I think I am doing something wrong with the template builder. Please see if you could help.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @Kalpana-3650,
I don't understand your descriptions clearly. Could you tell us what are repeated? Data? Or the template field?
Could you post your data or your result to us? It will be helpful.

Best regards,
Lan Huang

0 Votes 0 ·

Hi Lan Huang

Sorry for not providing the info earlier. The itemtemplate's data is the one that's getting repeated. See the image below, this specific list only has 3 items, but the 3 items are getting repeated 3 times.
127786-image.png



I think it's because the for loop, but how do I bind the item template dynamically when I create the control.

0 Votes 0 ·
image.png (57.6 KiB)

Hi @Kalpana-3650,
According to your description, I think your logic is right.
I suggest you replace rpt.ItemTemplate = itemTemplate; with rpt.AlternatingItemTemplate = itemTemplate;
Best regards,
Lan Huang





0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered

Again, I would use a GridView.

  public partial class _default : System.Web.UI.Page
     {
         public class ViewModelRecord
         {
             public int Id { get; set; }
             public string Name { get; set; }
         }
         protected void Page_Load(object sender, EventArgs e)
         {
             PopulateGridViews();
         }
    
         protected void PopulateGridViews()
         {
             List<List<ViewModelRecord>> tables = PopluateViewModelCollections();
             int i = 0;
             foreach(var table in tables)
             {
                 GridView gv = new GridView();
                 gv.ID = $"table{i++}";
                 gv.DataSource = table;
                 GridViewPlaceHolder.Controls.Add(gv);
                 gv.DataBind();
             }
         }
    
         protected List<List<ViewModelRecord>> PopluateViewModelCollections()
         {
             List<List<ViewModelRecord>> tables = new List<List<ViewModelRecord>>();
             for (int i = 0; i < 5; i++)
             {
                 List<ViewModelRecord> table = new List<ViewModelRecord>();
                 for (int j = 0; j < 5; j++)
                 {
                     table.Add(new ViewModelRecord() { Id = j, Name = $"item{i}-{j}" });    
                 }
                 tables.Add(table);
             }
    
             return tables;
         }
     }

Markup

     <form id="form1" runat="server">
         <div>
             <asp:PlaceHolder ID="GridViewPlaceHolder" runat="server"></asp:PlaceHolder>
         </div>
     </form>


127686-capture.png



capture.png (10.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.