Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Tuesday, March 09, 2010

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Populating a .Net Datagrid Control from a TreeNode Selection
8/5/2003 10:42:34 AM

This is a follow-up article to our "Populating a .Net Treeview control from a parent-child relationship db table." We will extend the previous article so that when someone selects a node in the tree it will populate a datagrid within the Winform.

In a previous article entitled "Populating a .Net Treeview control from a parent-child relationship db table." We discussed making a recursive query to a database and populating a treeview control based on the results. One of the comments left on the article was


"Now we need an article on examining the tree node by node once its populated AND an article on saving the tree to a table."


So in this article I will discuss selecting a node from the treeview and populating a grid control that matches the selected category that was selected.
 


If you remember on the treeview control from the previous article as we added nodes the tree from our dataset we also set the tag property for the node to be the key from the record in the database.


::


'set the tag property for the current node. This comes in useful if
            'you want to pass the value of a specific record id.
            'since the tag value is not visible, in the TreeView1_AfterSelect event
            'you could pass the value to another sub routine, for example:
  'FillDataGrid(TreeView1.SelectedNode.Tag)


            parentnode.Tag = parentrow.Item(0)
::


So now that the Tag property is set and contains the value of the primary key for the category record. We need to respond to an event, pass the primary key to our data grid and then populate the grid with the matching records. In this example our treeview is called TreeView1, we're going to trap the AfterSelect event and then fill the datagrid control.


Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal _
    e
As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    FillDataGrid(TreeView1.SelectedNode.Tag)
End
Sub


This calls a method "FillDataGrid", and you can see we're passing the currently selectednode.tag property to the method. In the method we'll make the parent ID an optional parameter. If no parent ID is passed we assume they all of the records in the database.

    Private Sub FillDataGrid(Optional ByVal inParentID As Integer = -1)
Dim MyDataSet As DataSet
Dim MyConnection As New _
SqlClient.SqlConnection("Server=server;Database=database;uid=user;pwd=pass;")

'we create our SQL statement which pulls all sites that match the parent category ID.
Dim SQLQ As String = "SELECT SiteID As ID, SiteName As Name, SiteURL As URL FROM Sites "
'here's we check the parent id and pull any matching sites.
'If there isn't a parent ID then we'll pull everything.

If inParentID <> -1 Then
SQLQ = SQLQ & "Where SiteCatID = " & inParentID
End If
Dim DACategories As New SqlClient.SqlDataAdapter(SQLQ, MyConnection)
MyDataSet = New DataSet
MyConnection.Open()
DACategories.Fill(MyDataSet, "Categories")
'Close the connection to the data store; free up the resources
MyConnection.Close()
Dim parentrow As DataRow
Dim ParentTable As DataTable
ParentTable = MyDataSet.Tables("Categories")
Dim i As Integer = 0
DataGrid1.DataSource = ParentTable
For Each parentrow In ParentTable.Rows
'so your application can still respond and doesn't hang.
Application.DoEvents()
If Trim(CType(DataGrid1.Item(i, 2), String)) = "" Then
DataGrid1.Item(i, 3) = "N/A"
End If
DataGrid1.Refresh()
i = i + 1
Next parentrow
End Sub


Now you should have a datagrid populated with the matching records for the parent ID in the database.

 

By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.

Related Articles
   Related Document Populating a .Net Treeview control from a parent-child relationship db table.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 7/25/2008 6:26:05 AM by Anonymous
Comments:
No ratings available.
Left on 2/23/2005 4:53:55 AM by Anonymous
Comments: why waste time writing an article if u can't answer questions
Left on 2/23/2005 4:48:34 AM by Anonymous
Comments: problem here is that the author can't be bothered to answer any question
Left on 8/19/2004 6:10:51 AM by Anonymous
Comments: Hi there, sorry to cramp your style but it's not working on mine. Firstly, even though the headings appear, the DGrid is still not populated; Secondly, the treeview doesn't expand.

Can you help me catch up.

Regards,
TT.
NB: Excellent resources!!
Left on 8/5/2004 8:37:15 AM by Anonymous
Comments: This really helped me understand the controls.Solved my problem precisely.
Left on 7/17/2004 7:03:12 AM by Anonymous
Comments: Hey! how do i populate if i have 2 treeviews ie., TreeView1 and TreeView2.
No ratings available.
Left on 6/6/2004 2:07:58 AM by Anonymous
Comments: To the point, clear, small and simple explanation and code.
Left on 3/12/2004 2:28:17 PM by Anonymous
Comments: Nice. Just what i needed
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970