Ok so for some reason I can't get the current item I selected in my datagrid to display on my textboxes

I'm new to vb net and programming itself so please bear with me.


here's my code:

Imports System.Data.OleDb
Public Class Form1
    Dim con As New OleDbConnection
    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim da As New OleDbDataAdapter
    Dim i As Integer


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con.ConnectionString = " Provider = microsoft.jet.oledb.4.0; data source = C:\Users\Judz\Desktop\userpass.mdb"

        con.Open()

        datagridShow()

    End Sub

    Private Sub datagridShow()

       
        ds.Tables.Add(dt)

        da = New OleDbDataAdapter("select * from userpass", con)

        da.Fill(dt)

        DataGridView1.DataSource = dt.DefaultView

        con.Close()



    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick


    End Sub

    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick



        TextBox1.Text = DataGridView1.Item(0, e.RowIndex).Value.ToString
        TextBox2.Text = DataGridView1.Item(1, e.RowIndex).Value.ToString
        TextBox3.Text = DataGridView1.Item(2, e.RowIndex).Value.ToString

    End Sub

I already tried other codes like

"TextBox2.Text = DataGridView1.CurrentCell.Value.ToString"

or

"TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString"

No changes whatsoever...

Please help me I'm really frustrated by this.

Thanks.

Recommended Answers

All 4 Replies

Try this :

Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim i As Integer = DataGridView1.CurrentRow.Index
        With DataGridView1
            TextBox1.Text = DataGridView1.Item(0, i).Value
            TextBox2.Text = DataGridView1.Item(1, i).Value
            TextBox3.Text = DataGridView1.Item(2, i).Value
        End With
    End Sub

hello !
i think you have to code at grid double or single click event . it will work fine :)

Regards

try this code from sql server - vb.net

Private Sub loddatagrid()
        Call main()
        Con.Open()

        DataAdapter1 = New SqlDataAdapter("SELECT [ID],[type],[lot],[loc],[meas],[units],[pounds] FROM [dbo].[Inventory] where [rcvandwork]='" & txt1.Text & "'", Con)
        DataSet1.Clear()
        DataAdapter1.Fill(DataSet1, "Table1")

        grid1.DataSource = DataSet1
        grid1.DataMember = "Table1"
        grid1.Refresh()
        Con.Close()
    End Sub

private sub form....
loddatagrid()
        txtno.DataBindings.Add("Text", DataSet1, "Table1.ID")
        xt1.DataBindings.Add("Text", DataSet1, "Table1.type")
        xt2.DataBindings.Add("Text", DataSet1, "Table1.lot")
        xt3.DataBindings.Add("Text", DataSet1, "Table1.loc")
        xt4.DataBindings.Add("Text", DataSet1, "Table1.meas")
        xt5.DataBindings.Add("Text", DataSet1, "Table1.units")
        xt6.DataBindings.Add("Text", DataSet1, "Table1.pounds")
end sub

try this code from sql server - vb.net

Private Sub loddatagrid()
        Call main()
        Con.Open()

        DataAdapter1 = New SqlDataAdapter("SELECT [ID],[type],[lot],[loc],[meas],[units],[pounds] FROM [dbo].[Inventory] where [rcvandwork]='" & txt1.Text & "'", Con)
        DataSet1.Clear()
        DataAdapter1.Fill(DataSet1, "Table1")

        grid1.DataSource = DataSet1
        grid1.DataMember = "Table1"
        grid1.Refresh()
        Con.Close()
    End Sub

private sub form....
loddatagrid()
        txtno.DataBindings.Add("Text", DataSet1, "Table1.ID")
        xt1.DataBindings.Add("Text", DataSet1, "Table1.type")
        xt2.DataBindings.Add("Text", DataSet1, "Table1.lot")
        xt3.DataBindings.Add("Text", DataSet1, "Table1.loc")
        xt4.DataBindings.Add("Text", DataSet1, "Table1.meas")
        xt5.DataBindings.Add("Text", DataSet1, "Table1.units")
        xt6.DataBindings.Add("Text", DataSet1, "Table1.pounds")
end sub

check this...I have done this and it has worked....

Dim i As Integer
        Dim DocID As String
        i = dgvDocSearch.CurrentRow.Index
        DocID = dgvDocSearch.Item(0, i).Value ' selects the first column of the row....
              'Connection string goes here 
        Try
            Dim myCommand As OleDbCommand

            myCommand = New OleDbCommand("SELECT DoctorID As ID ,(FirstName+' '+ LastName) as DoctorName  FROM DoctorRegister where DoctorID='" & DocID & "'", Connection) 'according to ur DocID it will take the value from database and display it in ur text box

            Dim reader As OleDbDataReader = myCommand.ExecuteReader
            While reader.Read
                Textboxname.Text = DocID
             
            End While
        Catch ex As Exception
            MsgBox("Error Connecting to Database: " & ex.Message)
        End Try
    'Close connection

this code has to be wriiten in datagrid cell click event
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.