bind data from datagrid to textbox

select data from datagrid to display in textbox

Page 1 of 1

2 Replies - 24753 Views - Last Post: 17 October 2010 - 10:29 AM Rate Topic: -----

#1 Suryati Akz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-February 10

bind data from datagrid to textbox

Posted 16 October 2010 - 08:17 PM

my code below doesn't work


 Private Sub DataGridView2_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Dim connect As OleDbConnection

        connect = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Car Rental Data\Mutiata Company Car Rental System1.mdb")
        connect.Open()

        Dim i As Integer
        cm = New OleDbCommand("SELECT * FROM CustomerDetails ", connect)

        i = DataGridView1.CurrentRow.Index
        dr = cm.ExecuteReader()
        TextBox30.Text = DataGridView1.Item(0, i).Value = dr("Name")
        TextBox27.Text = DataGridView1.Item(1, i).Value = dr("I/C Number")
        TextBox29.Text = DataGridView1.Item(2, i).Value = dr("Phone Number")
        TextBox26.Text = DataGridView1.Item(3, i).Value = dr("Address")
        TextBox28.Text = DataGridView1.Item(3, i).Value = dr("Poscode")
        TextBox25.Text = DataGridView1.Item(3, i).Value = dr("State")




i try to click select on the datagrid to be display into my textboxes. but unsuccessful.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: bind data from datagrid to textbox

#2 TEH  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 151
  • Joined: 30-January 08

Re: bind data from datagrid to textbox

Posted 17 October 2010 - 01:20 AM

If you want data from single row of datagridview to show up in textboxes, then you can set in every textbox-properties under databindings:
Tag-property to first column of your bindingsource

and Text-property to column witch value you want to show


If that post was helpful for you, please click + -sign below
Was This Post Helpful? 1
  • +
  • -

#3 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 84
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: bind data from datagrid to textbox

Posted 17 October 2010 - 10:29 AM

View PostSuryati Akz, on 16 October 2010 - 07:17 PM, said:

my code below doesn't work


 Private Sub DataGridView2_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Dim connect As OleDbConnection

        connect = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Car Rental Data\Mutiata Company Car Rental System1.mdb")
        connect.Open()

        Dim i As Integer
        cm = New OleDbCommand("SELECT * FROM CustomerDetails ", connect)

        i = DataGridView1.CurrentRow.Index
        dr = cm.ExecuteReader()
        TextBox30.Text = DataGridView1.Item(0, i).Value = dr("Name")
        TextBox27.Text = DataGridView1.Item(1, i).Value = dr("I/C Number")
        TextBox29.Text = DataGridView1.Item(2, i).Value = dr("Phone Number")
        TextBox26.Text = DataGridView1.Item(3, i).Value = dr("Address")
        TextBox28.Text = DataGridView1.Item(3, i).Value = dr("Poscode")
        TextBox25.Text = DataGridView1.Item(3, i).Value = dr("State")




i try to click select on the datagrid to be display into my textboxes. but unsuccessful.


Here is a quick example how to bind gridview with
textboxes, hope this will helps


Public Class Form1

    Dim dt As DataTable = New DataTable
    Dim curr As DataGridViewRow


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.DataGridView1.AllowUserToAddRows = False

        dt.Columns.AddRange(New DataColumn() _
                            {New DataColumn("Column1", GetType(Integer)), _
                             New DataColumn("Column2", GetType(String)), _
                             New DataColumn("Column3", GetType(Integer)), _
                             New DataColumn("Column4", GetType(Integer))})
        For i As Integer = 0 To 5
            dt.Rows.Add(i + 1, ChrW(i + 65), (i + 1) * 10, (i + 1) * 100)
        Next


        dt.TableName = "myTable"
        Dim ds As DataSet = New DataSet
        ds.Tables.Add(dt)

        ' using connection to database you can do it something like
        ' open connection , create SelectCommand for adapter then fill dataset
        'da.Fill(ds, "myTable")
        'da.TableMappings.Add("myTable", "myTable")
        'da.Update(ds, "myTable")

        Dim bind As BindingSource = New BindingSource(ds, "myTable")
        Me.DataGridView1.DataSource = bind

        Me.TextBox1.DataBindings.Add(New Binding("Text", bind, "Column1", False))'<-- change on True to save formatting
        Me.TextBox2.DataBindings.Add(New Binding("Text", bind, "Column2", False))
        Me.TextBox3.DataBindings.Add(New Binding("Text", bind, "Column3", False))
        Me.TextBox4.DataBindings.Add(New Binding("Text", bind, "Column4", False))

        curr = DataGridView1.CurrentRow

    End Sub

    Private Sub DataGridView1_Selectionchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Selectionchanged
        curr = Me.DataGridView1.CurrentRow
    End Sub

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
        curr = Me.DataGridView1.CurrentRow
        curr.Cells("Column1").Value = CInt(Me.TextBox1.Text)
        curr.Cells("Column2").Value = Me.TextBox2.Text
        curr.Cells("Column3").Value = CInt(Me.TextBox3.Text)
        curr.Cells("Column4").Value = CInt(Me.TextBox4.Text)
        dt.AcceptChanges()
        ' = add code to save changes to database here =
    End Sub
    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class


~'J'~

This post has been edited by fixo: 17 October 2010 - 10:31 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1