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.
|