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
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();
}
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>>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With