0

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.

1 Answer 1

3

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();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

but it doent work for showing different strings in different columns, it shows all strings in same column?
@vinaysingri - use TempleteField columns.
AVD you mean to say seperate template field tag for seperate literals is it??
@vinaysingri - Yes! that is true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.