Using a treeview to filter a database

Im trying to use a treeview so when you click on an item it filters a database to only view data that matches a filter.
Im usign a dataset that is viewed in a datagrid. I need the command so when you click on an item in the treeview i can filter that database.
ChikenDude911Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SRigneyCommented:
I'm not exactly sure what you're asking for here.  It sounds like you have a dataset that has a large chunk of data and there are set filters already in a treeview.  Then when the user clicks on one of the filters you want to display only data from the dataset that matches the filter criteria.

Here's a chunk of code straight from the .Net help
private static void GetRowsByFilter()
{
   
    DataTable customerTable = new DataTable( "Customers" );
    // Add columns
    customerTable.Columns.Add( "id", typeof(int) );
    customerTable.Columns.Add( "name", typeof(string) );

    // Set PrimaryKey
    customerTable.Columns[ "id" ].Unique = true;
    customerTable.PrimaryKey = new DataColumn[] { customerTable.Columns["id"] };

    // Add ten rows
    for( int id=1; id<=10; id++ )
    {
        customerTable.Rows.Add(
            new object[] { id, string.Format("customer{0}", id) } );
    }
    customerTable.AcceptChanges();

    // Add another ten rows
    for( int id=11; id<=20; id++ )
    {
        customerTable.Rows.Add(
            new object[] { id, string.Format("customer{0}", id) } );
    }

    string strExpr;
    string strSort;
   
    strExpr = "id > 5";
    // Sort descending by column named CompanyName.
    strSort = "name DESC";
    // Use the Select method to find all rows matching the filter.
    DataRow[] foundRows =
        customerTable.Select( strExpr, strSort, DataViewRowState.Added );
   
    PrintRows( foundRows, "filtered rows" );

    foundRows = customerTable.Select();
    PrintRows( foundRows, "all rows" );
}

private static void PrintRows( DataRow[] rows, string label )
{
    Console.WriteLine( "\n{0}", label );
    if( rows.Length <= 0 )
    {
        Console.WriteLine( "no rows found" );
        return;
    }
    foreach( DataRow r in rows )
    {
        foreach( DataColumn c in r.Table.Columns )
        {
            Console.Write( "\t {0}", r[c] );
        }
        Console.WriteLine();
    }
}
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button  in the upper-left corner of the page.

0
ChikenDude911Author Commented:
My only problem is that i cant set the treeview to filter.
0
SRigneyCommented:
I'm confused by what it is that your looking for here then.  Can you please give a little more detail of the process and what you are trying to accomplish?  Maybe there's a different way we can approach it.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ChikenDude911Author Commented:
Ok i am trying ot make an address book right.

Well i want it so under a tree view i can click friend, then next to the tree view it willshow the people that are my firends. i can then click on that friend to view its prrofile or edit the profile.
0
SRigneyCommented:
I think that you could add the filter groups that exist to your treeview, and then add the items that link to the actual people as subitems so when you click on them it would display the actual people.

If you are using 2 controls, which it almost sounds like you are, then you can bind a listview to the view of your dataset (I think) and when you set the filter property the listview will update itself.  I haven't done that, so I can't guarantee it will work, but it may.  Give me a little while and I'll play with some code to see what I can come up with.
0
ChikenDude911Author Commented:
i dont know how to be able to click a sub root and an action to occur
0
SRigneyCommented:
Sorry it took so long.  I had actual real work come up and had to take a detour from playing on the internet.  Here's a chunk of code that I think does what you are looking for.  I had to hard code a lot of things, but it works on my computer.  It uses a single treeview and a single datagrid.  There is no database connection because everything else is handled with hard coding.

    Dim customerTable As New System.Data.DataTable("Customers")
    Dim customerFilter As System.Data.DataTable
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Add columns
        customerTable.Columns.Add("id")
        customerTable.Columns.Add("name")
        customerTable.Columns.Add("group")
        ' Set PrimaryKey
        customerTable.Columns("id").Unique = True
        customerTable.PrimaryKey = New System.Data.DataColumn() {customerTable.Columns("id")}

        ' Add ten rows
        Dim id As Int32
        For id = 1 To 10
            customerTable.Rows.Add(New Object() {id, String.Format("customer{0}", id), "Friend"})
        Next

        ' Add another ten rows
        For id = 11 To 20
            customerTable.Rows.Add(New Object() {id, String.Format("customer{0}", id), "CoWorker"})
        Next
        customerTable.AcceptChanges()

        TreeView1.Nodes.Add("Friend")
        TreeView1.Nodes.Add("CoWorker")

    End Sub

    Private Sub CreateFilterTable(ByVal filter As String)
        customerFilter = New System.Data.DataTable("Customers")
        customerFilter.Columns.Add("id")
        customerFilter.Columns.Add("name")
        customerFilter.Columns.Add("group")
        ' Set PrimaryKey
        customerFilter.Columns("id").Unique = True
        customerFilter.PrimaryKey = New System.Data.DataColumn() {customerFilter.Columns("id")}

        filter = "group='" & filter & "'"

        Dim row As System.Data.DataRow
        Dim rowtoadd As System.Data.DataRow
        For Each row In customerTable.Select(filter)
            rowtoadd = customerFilter.NewRow()
            rowtoadd("id") = row("id")
            rowtoadd("name") = row("name")
            rowtoadd("group") = row("group")
            customerFilter.Rows.Add(rowtoadd)
        Next
        DataGrid1.DataSource = customerFilter
    End Sub

    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        CreateFilterTable(e.Node.Text)
    End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.