Solved

Custom selection from dataset

Posted on 2004-08-06
3
243 Views
Last Modified: 2010-04-15
Hi Experts,

I generate a dataset with 3 column: PathId, ParentPathId, Path.

Example:
PathId                            ParentPathId                      Path
1                                   null                                    path1
2                                   1                                       path2
3                                   2                                       path3
4                                   2                                       path4
5                                   null                                    path5
6                                   5                                       path6

What I want to get is those paths:
path1
path1/path2
path1/path2/path3
path1/path2/path4
path5
path5/path6

Thanks in advance to help me to retrieve those paths.
0
Comment
Question by:noulouk
3 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11740722
you are going to need to make a recursive function in order to get that, no compiler in front of me but this should be close.

public string GetPath(int ParentId) {
    DataView dv = new DataView(ds.Tables["whatever"]);
    dv.RowFilter = "PathId = " + ParentId;
    if(dv.Rows.Count == 1) {
          return dv.Rows[0].Item["Path"].ToString() + "/" + GetPath(dv.Rows[0].Item["PathId"];
    } else if(dv.Rows.Count > 1) {
          throw new System.Exception("More than one child directory found for " + ParentId);
    }
}

then to build all ...

public StringCollection GetAllPaths() {
      StringCollection sc = new StringCollection();
      DataView dv =  new DataView(ds.Tables["whatever"]);
      dv.RowFilter = "ParentPathId = null";
      foreach(DataRowView dr in dv.Rows) {
             sc.Add(GetPath((int) dr["PathId"]));
      }
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# LinkButton OnClientClick 2 34
Stream.BeginRead and Stream.EndRead in .NET Core 5 38
Runtime Error 2 29
Best resource to learn entity framework 2 10
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

30 Experts available now in Live!

Get 1:1 Help Now