locked
Repeater Display Issue RRS feed

  • Question

  • User1099429166 posted

    Dear All,

    I have a resultset that looks like this

    California 2018 v32
    Texas 2017 v31
    New Jersey 2017 v31
    Maine 2017 v31
    New York 2014 v31
    Alaska 2010 v29

    I am getting the resultset in the datatable and binding it with a repeater control. But the output is not right. I want the output to be as follows

    v32
         2018
         California

    v31
         2017
         Texas
         New Jersey
         Maine

        2014
        New York

    v29
        2010
        Alaska

    Any help would be greatly appreciated

    Tuesday, November 7, 2017 2:16 PM

All replies

  • User1099429166 posted

    I want a hierarchical display as I mentioned above 

    Tuesday, November 7, 2017 4:48 PM
  • User-707554951 posted

    Hi Sam solomon,

    Working sample as below:

     <asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="rptCustomers_ItemDataBound">
                    <HeaderTemplate>
                        <table>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <tr>
                            <td>
                                <asp:Label ID="lbldata" runat="server" Text='<%# Eval("data") %>' />
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:Repeater ID="detailrpt" runat="server" >
                                    <HeaderTemplate>
                                        <table>
                                           
                                    </HeaderTemplate>
                                    <ItemTemplate>   
                                         <tr>
                                                <th>
                                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Year") %>'></asp:Label></th>
                                            </tr>
                                        <tr>
                                            <td>
                                                <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Country") %>' />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td></td>
                                        </tr>
    
                                    </ItemTemplate>
                                    <FooterTemplate>
                                        </table>
                                    </FooterTemplate>
                                </asp:Repeater>
                            </td>
                        </tr>
                    </ItemTemplate>
                    <FooterTemplate>
                        </table>
                    </FooterTemplate>
                </asp:Repeater>

    Codebehind:

     DataTable dt = new DataTable();
           
            protected void Page_Load(object sender, EventArgs e)
            {
                dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Country"), new DataColumn("Year"), new DataColumn("data") });
                dt.Rows.Add("California", "2018", "v32");
                dt.Rows.Add("Texas", "2017", "v31");
                dt.Rows.Add("New Jersey", "2017", "v31");
                dt.Rows.Add("Maine", "2017", "v31");
                dt.Rows.Add("New York", "2014", "v31");
                dt.Rows.Add("Alaska", "2010", "	v29");
                DataView view = new DataView(dt);
                DataTable distinctValues = view.ToTable(true, "data");
                rptCustomers.DataSource = distinctValues;
                rptCustomers.DataBind();
                
                foreach (RepeaterItem item in rptCustomers.Items){
                    Repeater nestedrepeater = (Repeater)item.FindControl("detailrpt");
                    List<int> pp = new List<int>();
                    for (int i = 0; i < nestedrepeater.Items.Count-1; i++)
                    {
                        string aa = ((Label)nestedrepeater.Items[i].FindControl("Label1")).Text;
                        string bb = ((Label)nestedrepeater.Items[i + 1].FindControl("Label1")).Text;
                        if (aa == bb)
                        {
                            pp.Add(i+1);
                            //((Label)nestedrepeater.Items[i + 1].FindControl("Label1")).Style.Add("display", "none");
    
                        }
                    }
                    foreach(var index in pp)
                    {
    
                        ((Label)nestedrepeater.Items[index].FindControl("Label1")).Visible=false;
                    }
                   
                }
              
    
    
            }
    
            protected void rptCustomers_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if(e.Item.ItemType==ListItemType.AlternatingItem|| e.Item.ItemType == ListItemType.Item)
                {
                    string  data = ((Label)e.Item.FindControl("lbldata")).Text;
                    Repeater nestedrepeater = (Repeater)e.Item.FindControl("detailrpt");
                    var result =dt.AsEnumerable().Where(dr => dr.Field<string>("data") == data);
                    DataTable newdt = new DataTable();
                    newdt.Columns.AddRange(new DataColumn[3] { new DataColumn("Country"), new DataColumn("Year"), new DataColumn("data") });
                    foreach (DataRow  row in result)
                    {
                        newdt.Rows.Add(row.ItemArray);
    
                    }
               
                    nestedrepeater.DataSource = newdt;
                    nestedrepeater.DataBind();
    
    
                }
            }

    Output:

    Best regards

    Cathy

    Thursday, November 9, 2017 10:59 AM
  • User-359936451 posted

    Here are two links that should help you a lot!!!

    https://msdn.microsoft.com/en-us/library/bb510136.aspx

    https://www.aspsnippets.com/Articles/Repeater-control-Tutorial-with-example-in-ASPNet-using-C-and-VBNet.aspx

    Look at Steps 1 and Step 2 regarding formatting in the first link. It should help answer your question better with a clear explanation on what to do.

    Friday, November 10, 2017 3:43 PM