Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind a list with one key and multiple values to a GridView or a Repeater

enter image description here

and I am having a function to convert this list to a datatable

public static DataTable ConvertToDatatable<T>(IList<T> data)
        {
            PropertyDescriptorCollection props =
                TypeDescriptor.GetProperties(typeof(T));
            DataTable table = new DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name, prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

what should I do to bind these multiple values to a repeater or a gridview

like image 571
Dennis Avatar asked Jan 09 '23 16:01

Dennis


2 Answers

You can take repeater or gridview inside parent gridview or repeater.

for example:-

     Dictionary<int, object> g = new Dictionary<int, object>() { { 1, new { j = "g" } }, { 2, new { j = "ggfdf" } }, { 3, new { j = "gioret" } } };


  gridviewparent.DataSource= g.Select(h => new {key= h.Key});
  gridviewparent.DataBind();



  foreach (GridViewRow item in gridviewparent.Rows)
        {
         int key= Convert.Toint32( ((Label)item.FindControl("lblkey")).Text) ;
               GridView gridviewChild=(GridView)item.FindControl("gridviewchild");
            // return g[key]  in list
            gridviewChild.DataSource= g[key];
            gridviewChild.DataBind();

        } 
like image 54
pradeep varma Avatar answered Jan 12 '23 05:01

pradeep varma


You could use a repeater inside a repeater. Here's how:

Convert your List into a Dictionary<string, List<object>> and store it into a Property (in my sample it is named Dict) that can be accessed by the .aspx file (at least protected or more accessible).
Then call outerRepeater.DataBind() at some point (For example in your Page_Load(object sender, EventArgs e) implementation).

Put this into your .aspx file to make it work:

<asp:Repeater runat="server" ID="outerRepeater" DataSource='<%# Dict.Keys%>'>
<ItemTemplate>
    <asp:Repeater runat="server" ID="innerRepeater" DataSource='<%# Dict[DataBinder.Eval(Container, "DataItem").ToString()]%>'>
        <ItemTemplate>
            <%# DataBinder.Eval(Container, "DataItem")%>
        </ItemTemplate>
    </asp:Repeater>
</ItemTemplate>

You can use <%# DataBinder.Eval(Container, "DataItem.EventDate")%> to access a Property (in this sample with the name EventDate) when you store concrete objects in the List inside the dictionary. For example: Dictionary<string, List<Event>>

like image 20
krizzzn Avatar answered Jan 12 '23 05:01

krizzzn