Link text boxes with listview selected item vb.net

Khaled ali 0 Reputation points
2023-03-04T23:59:42.3366667+00:00

I have a Visual Basic.net project with Database

(Column names = Textbox Name)

(NEM=TxtBook - PON=TxtPublisher-ANON=TxtPublish-DEN=TxtAuthor-TXTC=TxtNote-DATAC=TxtPage)

I made the listbox show only one column (NEM) from the table (castmer)Untitled

I want all other column data to appear in the textboxes when I select an item from the listbox Even though the data gridview is present, it is linked correctly to the text boxes Please solve Thank you all

I tried several solutions and it didn't work

Developer technologies VB
{count} votes

2 answers

Sort by: Most helpful
  1. LesHay 7,141 Reputation points
    2023-03-05T00:55:16.2066667+00:00

    Hi

    Very difficult to help when you show no code at all.

    However, here is some code that may help. This is a stand alone example. If you want to try it, start a new project and add a ListBox1, DataGridView1, TextBoxes 1,2,3,4 and 5

    The DataTable is created with dummy data just for demo purposes. When run, the data is bound to all the controls so that selecting an item int either the ListBox or the DataGridView, the TextBoxes will show the data accordingly.

    111

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim dt As New DataTable("Freddy")
      Dim BS As New BindingSource
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With dt
          .Columns.Add("NEM", GetType(String))
          .Columns.Add("DEN", GetType(String))
          .Columns.Add("PON", GetType(String))
          .Columns.Add("ANON", GetType(String))
          .Columns.Add("DATAC", GetType(String))
          ' some dummy data
          For i As Integer = 0 To 9
            .Rows.Add("NEM" & i.ToString, "DEN" & i.ToString, "PON" & i.ToString, "ANON" & i.ToString, "DATAC" & i.ToString)
          Next
        End With
        BS.DataSource = dt
        DataGridView1.DataSource = BS
        With ListBox1
          .DataSource = BS
          .DisplayMember = "NEM"
        End With
        TextBox1.DataBindings.Add("Text", BS, "NEM")
        TextBox2.DataBindings.Add("Text", BS, "DEN")
        TextBox3.DataBindings.Add("Text", BS, "PON")
        TextBox4.DataBindings.Add("Text", BS, "ANON")
        TextBox5.DataBindings.Add("Text", BS, "DATAC")
      End Sub
    End Class
    
    

  2. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-03-09T06:20:06.25+00:00

    Hi, welcome to Microsoft Q&A.

    If you added a database table as a datasource directly to the Datagridview, you can refer to the following code.

    You can try applying it to your project.

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: This line of code loads data into the 'TestDB1DataSet1.Table' table. You can move, or remove it, as needed.
            Me.TableTableAdapter.Fill(Me.TestDB1DataSet1.Table)
    
            With ListBox1
                .DataSource = DataGridView1.DataSource
                .DisplayMember = "NEM"
            End With
            TxtBook.DataBindings.Add("Text", DataGridView1.DataSource, "NEM")
            TxtAuthor.DataBindings.Add("Text", DataGridView1.DataSource, "DEN")
            TxtPublisher.DataBindings.Add("Text", DataGridView1.DataSource, "PON")
            TxtPublish.DataBindings.Add("Text", DataGridView1.DataSource, "ANON")
            TxtPage.DataBindings.Add("Text", DataGridView1.DataSource, "DATAC")
            TxtNote.DataBindings.Add("Text", DataGridView1.DataSource, "TXTC")
        End Sub
    

    Best Regards.
    Jiachen Li
    ----------
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.