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.
Add a comment
|
1 Answer
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();
}
}
4 Comments
vinay singri
but it doent work for showing different strings in different columns, it shows all strings in same column?
KV Prajapati
@vinaysingri - use TempleteField columns.
vinay singri
AVD you mean to say seperate template field tag for seperate literals is it??
KV Prajapati
@vinaysingri - Yes! that is true.