Izzy
Izzy

Reputation: 6876

Using Multiple GridViews

I'm currently working on a website and one of the pages I have a requirement to display data in different GridView. That pages looks something like

<h2>First Header</h2>
<p>Some text</p>
<asp:GridView ID="FirstGrid" runat="server"/>
...
<h2>Second Header</h2>
<p>Some text</p>
<asp:GridView ID="SecondGrid" runat="server"/>
...
<h2>Third Header</h2>
<p>Some text</p>
<asp:GridView ID="ThirdGrid" runat="server"/>
...

In total I will have around 6-7 Gridviews. Each grid displays files from a different directory. Currently I have a function BindGridView(string directoryName) and I do the following for each grids DataSource

FirstGrid.DataSource = BindGridView("First Directory");
FirstGrid.DataBind();
SecondGrid.DataSource = BindGridView("Second Directory");
SecondGrid.DataBind();
...

My question is can I have one Gridview and change the DataSource or will I need all 6-7 and keep on working the way I am.

The image below is what I have so far.. Above the File Name is a main header for each grid

enter image description here

Thanks in advance for your help

Upvotes: 0

Views: 279

Answers (2)

codeandcloud
codeandcloud

Reputation: 55298

Here is a quick and dirty example that use nested ListView.

Here I am using two classes Directory and DirectoryFile.

namespace WebFormsApp
{
    public class Directory
    {
        // the directory names like First Directory
        public string DirectoryName { get; set; }
        // the content that comes under header tag
        public string HeaderText { get; set; }
        // the content that comes under p tag
        public string InfoText { get; set; }
    }
    public class DirectoryFile
    {
        // file name
        public string FileName { get; set; }
        // download url
        public string Url { get; set; }
    }
}

The markup should like something like

<asp:ListView ID="DirectoryList" runat="server" 
    ItemType="WebFormsApp.Directory" 
    SelectMethod="GetDirectories"
    OnItemDataBound="DirectoryList_ItemDataBound">   
    <ItemTemplate>
        <h2><%# Item.HeaderText %></h2>
        <p><%# Item.InfoText %></p>
        <asp:ListView ID="FileList" runat="server" 
            DataMember='<%# Item.DirectoryName %>' 
            ItemType="WebFormsApp.DirectoryFile">
            <LayoutTemplate>
                <table>
                    <thead>
                        <tr>
                            <th>File Name</th>
                            <th>Download</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr id="itemPlaceHolder" runat="server"></tr>
                    </tbody>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td><%# Item.FileName %></td>
                    <td><%# Item.Url %></td>
                </tr>
            </ItemTemplate>
        </asp:ListView>
    </ItemTemplate>
</asp:ListView>

Here the parent ListView has a SelectMethod="GetDirectories" that excepts ItemType="WebFormsApp.Directory". The method will be

public List<Directory> GetDirectories()
{
    //assuming it will be a not populated from database. 
    // if so change your code accordingly
    var directories = new List<Directory>()
        {
            new Directory {DirectoryName="FirstDirectory", HeaderText="First Header", InfoText="Some Info"},
            new Directory {DirectoryName="SecondDirectory", HeaderText="Second Header", InfoText="Other Info"}
        };
    return directories;
}

Lastly hookup an ItemDataBound method ( it iterates through each item ) and find the child ListView. Please see that we have provided a DataMember='<%# Item.DirectoryName %>' so that we can access it at code-behind.

protected void DirectoryList_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        var listView = (ListView)e.Item.FindControl("FileList");
        var directoryName = listView.DataMember;
        GetFiles(listView, directoryName);
    }
}
public void GetFiles(ListView listView, string directoryName)
{

    listView.DataSource = BindChildlistView(directoryName);
    listView.DataBind();
}

Hope the code is self-explanatory

Upvotes: 1

jomargon
jomargon

Reputation: 169

Just use nested repeaters if that's the case. Unless you really need the GridView built-in features such as sorting, etc.

Upvotes: 0

Related Questions