Solved

Using a treeview to filter a database

Posted on 2003-11-20
10
244 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

708 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

19 Experts available now in Live!

Get 1:1 Help Now