Solved

Custom selection from dataset

Posted on 2004-08-06
3
249 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Facial recognition to look through the whole database for a person 3 61
Coding C# in Linux 8 60
2 questions 10 25
VB: Convert 2 dates to specific format 24 48
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

18 Experts available now in Live!

Get 1:1 Help Now