Bj Molo Ответов: 1

Как отправить выбранное значение treeview в текстовые поля


I have a access database with two tables namely Tblreqs and Tbllib, I used Tblreqs to populate into treeview, i want to use the selected node to fill textboxes with data from Tbllib.
Here are the codes have tried so far 


Что я уже пробовал:

Imports System.Data.OleDb
Public Class Form1

    Private Sub TblreqsBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.TblreqsBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.CopycopyDataSet)

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CopycopyDataSet.tbllib' table. You can move, or remove it, as needed.
        Me.TbllibTableAdapter.Fill(Me.CopycopyDataSet.tbllib)
        'TODO: This line of code loads data into the 'CopycopyDataSet.tbllib' table. You can move, or remove it, as needed.
        Me.TbllibTableAdapter.Fill(Me.CopycopyDataSet.tbllib)
        'TODO: This line of code loads data into the 'CopycopyDataSet.tbllib' table. You can move, or remove it, as needed.
        Me.TbllibTableAdapter.Fill(Me.CopycopyDataSet.tbllib)
        'TODO: This line of code loads data into the 'CopycopyDataSet.tblreqs' table. You can move, or remove it, as needed.
        Me.TblreqsTableAdapter.Fill(Me.CopycopyDataSet.tblreqs)

        GroupTextBox.Text = " "
        DetailsTextBox.Text = " "
        RefnoTextBox.Text = " "
        QtyTextBox.Text = " "
        RateTextBox.Text = " "
        UnitTextBox.Text = " "
        TotalTextBox.Text = " "

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TblreqsTableAdapter.Connection.Open()
        TreeView1.Nodes.Clear()
        FillTree("1", "Library", Nothing)
        TblreqsTableAdapter.Connection.Close()
    End Sub

    Public Sub FillTree(ByVal Key As String, ByVal Txt As String, ByVal N As TreeNode)
        Dim cn As OleDbConnection
        Dim cmd As OleDbCommand
        Dim NN As TreeNode
        If N Is Nothing Then
            NN = TreeView1.Nodes.Add(Key, Txt)
        Else
            NN = N.Nodes.Add(Key, Txt)
        End If
        cn = TblreqsTableAdapter.Connection
        cmd = New OleDbCommand("select * from tblreqs where idparent='" & Key & "'", cn)
        Dim dr = cmd.ExecuteReader
        Do While dr.Read()
            FillTree(dr("id"), dr("detail"), NN)
        Loop
        dr.Close()
        cmd.Dispose()
    End Sub

    Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect

        Dim cn As OleDbConnection
        Dim wybrany As String
        wybrany = e.Node.Text
        cn = TblreqsTableAdapter.Connection
        Dim cmd As New OleDbCommand
        Dim dat As OleDbDataReader
        Dim querry As String = "select Refno, Group, Details, Qty, Rate, Total, Unit from tbllib where (detail) = '" + wybrany + "'"
        cmd = New OleDbCommand(querry, cn)
        dat = cmd.ExecuteReader
        While dat.Read()

            RefnoTextBox.Text = dat.Item(0).ToString
            GroupTextBox.Text = dat.Item(1).ToString
            DetailsTextBox.Text = dat.Item(2).ToString
            QtyTextBox.Text = dat.Item(3).ToString
            RateTextBox.Text = dat.Item(4).ToString
            TotalTextBox.Text = dat.Item(5).ToString
            UnitTextBox.Text = dat.Item(6).ToString

        End While
    End Sub

End Class

1 Ответов

Рейтинг:
2

Sandeep Mewara

Я не вижу, где вы использовали SelectedNode собственность.

Взгляните на детали здесь: TreeView элемент.Свойство SelectedNode (System.Окна.Формы) | Microsoft Docs[^]

Образец:

Private Sub myButton_Click(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles myButton.Click
   ' Set the tree view's PathSeparator property.
   myTreeView.PathSeparator = "."

   ' Get the count of the child tree nodes contained in the SelectedNode.
   Dim myNodeCount As Integer = myTreeView.SelectedNode.GetNodeCount(True)
   Dim myChildPercentage As Decimal = CDec(myNodeCount) / _
      CDec(myTreeView.GetNodeCount(True)) * 100

   ' Display the tree node path and the number of child nodes it and the tree view have.
   MessageBox.Show(("The '" + myTreeView.SelectedNode.FullPath + "' node has " _
      + myNodeCount.ToString() + " child nodes." + Microsoft.VisualBasic.ControlChars.Lf _
      + "That is " + String.Format("{0:###.##}", myChildPercentage) _
      + "% of the total tree nodes in the tree view control."))
End Sub

Как и выше, как только вы узнаете свой выбранный узел, используйте способ, который вы ищете (поместите в текстовое поле, как вы поделились)


Теперь, переходя к критической вещи, ваш код открыт для SQL-инъекция. Вы должны использовать parametrized query для доступа к данным. Читать об этом:
SQL-инъекция[^]
Доступ к данным с помощью ADO.NET[^]
Как выполнять параметризованные запросы - SQL Server | Microsoft Docs[^]