Bind List of array of objects in ListView in ASP.NET
I am racking my brains to fix the problem. I have a method that returns List<Object[]>
.
Each object[]
in the List contains the following:
object[0]=Id;
object[1]=Name;
Now I'm looking for a way to bind this list to a ListView in a custom ItemTemplate
one that looks like this:
<asp:Label runat="server" ID="lblId"
Text="Here want to do an Eval/Bind for object[0]"></asp:Label>
<asp:Label runat="server" ID="lblName"
Text="Here want to do an Eval/Bind for object[1]"></asp:Label>
Any suggestions would be deeply appreciated.
source to share
Your datasource is not capable of standard data binding. Convert it to a name value pair that will have a name and value for each element that will be bound. For example, the Dictionary <string, string> collection is compatible for this. And then just include the ListView:
<asp:Label runat="server" ID="lblId"
Text='<%# Eval("Key") %>'></asp:Label>
<asp:Label runat="server" ID="lblName"
Text='<%# Eval("Value") %>'></asp:Label>
source to share
A list of arrays of objects is a poor choice for storing elements. You should consider using a class representing an element, or a dictionary as suggested by @Canavar. Then you can use the Eval method in a cleaner way.
However, it can be linked to your current setup, although the syntax is making my eyes bleed.
<asp:Label runat="server" ID="lblId"
Text='<%# ((Object[])Container.DataItem)[0] %>' />
<asp:Label runat="server" ID="lblName"
Text='<%# ((Object[])Container.DataItem)[1] %>' />
source to share