rgshenoy
rgshenoy

Reputation: 449

Bind a List<DataTable> to a ListView with GridView in ItemTemplate

I have a List of DataTables I generate. This number of items in this list is variable. How do I bind the datasource of each of the gridview inside the listview to a datatable in this list? I have tried setting the datasource of the listview, but this doesn't help. Any suggestions would be appreciated.

Upvotes: 1

Views: 1155

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460108

You could assign the List<DataTable> to the DataSource property of the ListView. Then assign the DataTable to the DataSource property of the GridView in the ListView's ItemDataBound event:

protected void ListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        DataTable table = (DataTable) e.Item.DataItem;
        GridView grid = (GridView) e.Item.FindControl("GridViewID");
        grid.DataSource = table;
        grid.DataBind();
    }
}

Upvotes: 1

Related Questions