Fill a tree view with a stored procedure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john wright

    #1

    Fill a tree view with a stored procedure

    I have a stored procedure that returns a dataset. I want this dataset to
    fill a tree view.

    Here is the Stored Procedure Query

    SELECT Product.Product _#, Product.Part_Nu mber, Status.Status,
    Traveler_Step.S tep, Station.Station _Name
    FROM Product INNER JOIN
    Traveler_Step ON Product.Travele r_Step_# =
    Traveler_Step.T raveler_Step_# INNER JOIN
    Station ON Traveler_Step.S tation_# = Station.Station _#
    INNER JOIN
    Status ON Product.Status_ # = Status.Status_#
    WHERE (Station.Statio n_# = 8)


    Here is a typical result

    Product_# Part_Number Status Step
    Station_Name
    ----------- -------------------- -------------------- ----------- --------------------
    48 123789 Active 40 Hot Mill
    231 U06107LG Active 20 Hot Mill


    I would like the tree view master node to be Station Name, the first child
    node to be the Part Number with an icon next to it indicating the status,
    next child node under part_number the Step and Product_#.

    So this is what I am after

    HotMill
    |
    -----(Active icon) 123789
    |
    ------40
    |
    ------48

    I understand I need hierarchical data, but the stored procedure only returns
    one dataset. How can I do this?

    Thanks.

    John


  • Aboulfazl Hadi

    #2
    Re: Fill a tree view with a stored procedure

    Hi John
    Suppose you populate your dataTable and you are ready to fill your tree
    using FillTree :

    private void FillTree(DataTa ble table)
    {
    foreach (DataRow row in table.Rows)
    AddRow(row);
    }

    private void AddRow(DataRow row)
    {
    TreeNode stationNode =
    FindStationNode (row["Station_Na me"].ToString());
    if (stationNode == null)
    AddStationNode( row["Station_Na me"]);

    AddToStation(st ationNode, row);
    }

    private void AddToStation(Tr eeNode stationNode, DataRow row)
    {
    TreeNode node = new TreeNode();
    node.Text = row["Part_Numbe r"].ToString();
    if (row["Status"].ToString() == "Active")
    node.ImageIndex = 0;
    else
    node.ImageIndex = 1;
    node.Nodes.Add( row["Step"].ToString());
    node.Nodes.Add( row["Product_#"].ToString());

    stationNode.Nod es.Add(node);
    }

    private void AddStationNode( string name)
    {
    treeView1.Nodes .Add(name);
    }

    private TreeNode FindStationNode (string p)
    {
    foreach (TreeNode node in treeView1.Nodes )
    if (node.Text == p)
    return node;
    return null;
    }

    Best Regards,
    A.Hadi

    Comment

    • Aboulfazl Hadi

      #3
      Re: Fill a tree view with a stored procedure

      I'm sorry.I thought I am in C# group. (VB version) :
      Private Sub FillTree(ByVal table As DataTable)

      For Each row As DataRow In table.Rows
      AddRow(row)
      Next
      End Sub

      Private Sub AddRow(ByVal row As DataRow)

      Dim stationNode As TreeNode =
      FindStationNode (row("Station_N ame").ToString( ))
      If stationNode Is Nothing Then
      AddStationNode( row("Station_Na me"))
      End If
      AddToStation(st ationNode, row)
      End Sub

      Private Sub AddToStation(By Val stationNode As TreeNode, ByVal row
      As DataRow)

      Dim node As TreeNode = New TreeNode()
      node.Text = row("Part_Numbe r").ToString ()
      If (row("Status"). ToString() = "Active") Then
      node.ImageIndex = 0
      Else
      node.ImageIndex = 1
      node.Nodes.Add( row("Step").ToS tring())
      node.Nodes.Add( row("Product_#" ).ToString())
      End If

      stationNode.Nod es.Add(node)
      End Sub

      Private Sub AddStationNode( ByVal name As String)

      TreeView1.Nodes .Add(name)
      End Sub

      Private Function FindStationNode (ByVal name As String) As TreeNode

      For Each node As treenode In treeView1.Nodes
      If node.Text = name Then
      Return node
      End If
      Next
      Return Nothing
      End Function

      Thanks

      Comment

      Working...