Solved

Using a treeview to filter a database

Posted on 2003-11-20
10
246 Views
Last Modified: 2010-05-03
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.
0
Comment
Question by:ChikenDude911
  • 4
  • 3
10 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 9790351
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
 

Author Comment

by:ChikenDude911
ID: 9797251
My only problem is that i cant set the treeview to filter.
0
 
LVL 15

Expert Comment

by:SRigney
ID: 9797347
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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

Author Comment

by:ChikenDude911
ID: 9797681
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
 
LVL 15

Expert Comment

by:SRigney
ID: 9797929
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
 

Author Comment

by:ChikenDude911
ID: 9798100
i dont know how to be able to click a sub root and an action to occur
0
 
LVL 15

Accepted Solution

by:
SRigney earned 150 total points
ID: 9801432
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

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

828 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