This is a migrated thread and some comments may be shown as answers.

Grid Hierarchy with unknown -unlimited- hierarchy levels

3 Answers 42 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ahmed
Top achievements
Rank 1
ahmed asked on 18 Jul 2011, 04:01 PM
I'm making  a grid that displays file system directories and files as a hierarchy
The problem is it only works fine for only the first level of hierarchy, what I should do to make this supports unlimited levels.

here is my code:
ASPX:
<div>
        <telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowEdit="True"
            AutoGenerateHierarchy="True" CellSpacing="0" GridLines="None"
            Skin="Windows7" ondetailtabledatabind="RadGrid1_DetailTableDataBind">
            <MasterTableView  DataKeyNames="FullName,IsDirectory,ParentDirectory">
                <DetailTables>
                    <telerik:GridTableView  AutoGenerateColumns="true" DataKeyNames="FullName,IsDirectory,ParentDirectory">
                    </telerik:GridTableView>
                </DetailTables>
            </MasterTableView>
        </telerik:RadGrid>
    </div>


Code File:
public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            List<FileWrapper> dirs  = new DirectoryInfo("C://").GetDirectories().Select(obj => new FileWrapper(obj, true)).ToList();
            List<FileWrapper> files = new DirectoryInfo("C://").GetFiles().Select(obj => new FileWrapper(obj, true)).ToList();
            dirs.AddRange(files);
            RadGrid1.DataSource = dirs;
            RadGrid1.DataBind();
        }
 
    }
 
    protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
 
        GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
 
        bool isDirectory = bool.Parse(dataItem.GetDataKeyValue("IsDirectory").ToString());
        string FullName = dataItem.GetDataKeyValue("FullName").ToString();
 
        if (isDirectory)
        {
            DirectoryInfo d = new DirectoryInfo(FullName);
            List<FileWrapper> dirs = d.GetDirectories().Select(obj => new FileWrapper(obj, false)).ToList();
            List<FileWrapper> files = d.GetFiles().Select(obj => new FileWrapper(obj, false)).ToList();
            dirs.AddRange(files);
            e.DetailTableView.DataSource = dirs;
        }
    }
 
 
 
 
}
 
 
 
public class FileWrapper
{
    public string Name { get; set; }
    public string FullName { get; set; }
    public bool IsDirectory { get; set; }
    public DateTime CreationTime { get; set; }
    public string ParentDirectory { get; set; }
    public List<FileWrapper> Children { get; set; }
 
    public FileWrapper(object o, bool isRoot)
    {
 
        if (o is FileInfo)
        {
            FileInfo f = (FileInfo)o;
            Name = f.Name;
            FullName = f.FullName;
            CreationTime = f.CreationTime;
            IsDirectory = false;
            if (!isRoot)
            {
                ParentDirectory = f.Directory.FullName;
            }
             
        }
 
        if (o is DirectoryInfo)
        {
            DirectoryInfo d = (DirectoryInfo)o;
            Name = d.Name;
            FullName = d.FullName;
            CreationTime = d.CreationTime;
            IsDirectory = true;
            if (!isRoot)
            {
                ParentDirectory = d.Parent.FullName;
            }
             
        }
    }
}

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 19 Jul 2011, 05:58 AM
Hello Ahmed,

You can try the same approach in the following demo for achieving this.
Grid / Programmatic Binding.

Thanks,
Princy.
0
Maurizio
Top achievements
Rank 1
answered on 25 Nov 2013, 04:42 PM
How can we set the grid to have unlimited sub-grid if the only way (the only way I know) is to write the DetailTable in the HTML markup?
Do we have to set it during runtime? Is there a sample that accomplish this scenario?
Thank you
0
Pavlina
Telerik team
answered on 28 Nov 2013, 06:57 PM

Hi Maurizio,

In order to display hierarchical data, Telerik's ASP.NET Grid component renders one or more detail tables for each item in the MasterTableView (i.e. for each data-row that MasterTableView renders). How to bind each detail table-view declaratively or programmatically through DetailTableDataBind event is demonstrated in the examples below:
http://demos.telerik.com/aspnet-ajax/grid/examples/hierarchy/declarative-relations/defaultcs.aspx
http://demos.telerik.com/aspnet-ajax/grid/examples/data-binding/programmatic-hierarchy/defaultcs.aspx

Regards,

Pavlina
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
Grid
Asked by
ahmed
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Maurizio
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or