vinay singri
vinay singri

Reputation: 199

Binding List<Collection<string>> To grid view in asp.net

I have a List<Collection<string>>object with 10,000 objects in it and I want to show those strings as report(in a grid view), but binding the object directly to grid produces me no result.So can anyone help me out as to how to bind the collection of strings as different columns with the header name that I need.

Upvotes: 0

Views: 3419

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94653

You may use [] index to bind the dataSource (List of string/array) item.

Markup:

<asp:GridView ID="GridView1" 
              runat="server" 
              AutoGenerateColumns="False">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Literal 
                        ID="Literal1" 
                        runat="server"
                        Text='<%#Eval("[0]") %>'
                        >
                </asp:Literal> 
                <asp:Literal 
                        ID="Literal2" 
                        runat="server"
                        Text='<%#Eval("[1]") %>'
                        >
                </asp:Literal>   
                <asp:Literal 
                        ID="Literal3" 
                        runat="server"
                        Text='<%#Eval("[2]") %>'
                        >
                </asp:Literal> 
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<List<string>> list = new List<List<string>>()
        {
                new List<string>() {"A","B","C" },
                new List<string>() { "P","Q","R"}
        };
        GridView1.DataSource = list;
        GridView1.DataBind();
    }
}

Upvotes: 3

Related Questions