how to transfer data from datagrid to textbox using vb6.0????

Dani AI

Generated

Quick clarification first: VB6 has two common grid families and the appropriate technique depends on which one is on the form. MSFlexGrid / VSFlexGrid (an unbound grid) is typically manipulated with properties like TextMatrix; the bound Microsoft DataGrid control is driven by an ADODB recordset or an ADODC data control and is best handled via data-binding or the grid’s recordset/bookmark APIs. (vb-helper.com)

Two recommended, robust patterns for “click a row/lastname and populate textboxes”:

  • Data binding (simple, automatic updates)

    Bind each TextBox to the same data source and field so the control system keeps the boxes synced with the selected row. Example (run-time binding):

    Set txtLastName.DataSource = Adodc1
    txtLastName.DataField = "LastName"

    This avoids manual copying and keeps edits in sync with the recordset. (flylib.com)

  • Read the bound Recordset (explicit copy)

    Handle the grid’s selection event (RowColChange or Click), get the bound Recordset (either DataGrid1.DataSource or DataGrid1.DataSource.Recordset depending on how it was bound), set the recordset’s Bookmark to the grid’s Bookmark, then copy fields into the TextBoxes. Example pattern:

    ' inside DataGrid1_RowColChange or DataGrid1_Click
    Dim rs As ADODB.Recordset
    If TypeOf DataGrid1.DataSource Is ADODB.Recordset Then
        Set rs = DataGrid1.DataSource
    Else
        Set rs = DataGrid1.DataSource.Recordset
    End If
    rs.Bookmark = DataGrid1.Bookmark
    txtLastName.Text = IIf(IsNull(rs!LastName), "", CStr(rs!LastName))
    txtFirstName.Text = IIf(IsNull(rs!FirstName), "", CStr(rs!FirstName))

    This is safe for single- or multi-select scenarios (SelBookmarks) and works even when the grid is rearranged. (daniweb.com)

Troubleshooting / tips: add a reference to the Microsoft ActiveX Data Objects library when using ADODB; prefer field names (rs!FieldName) over numeric column indexes; always guard against Nulls before CStr/CInt; if multi-row selection is required use the SelBookmarks collection and process each bookmark via rs.Bookmark. (daniweb.com)

Notes tied to earlier replies: ’s TextMatrix approach is appropriate for an MSFlexGrid-style control; for a bound DataGrid the two patterns above are more robust and maintainable.

Recommended Answers

All 6 Replies

You can use the following -

Text1.Text = Datagrid1.TextMatrix(Datagrid1.Row, 1) 'The 1 at the end refers to the coloumn value in the grid, in this case coloumn 1...

how about when you click for example the lastname then it will transfer the whole data on the datagrid to the textboxes

Use the same code, just change the coloumn values -

Text1.Text = Datagrid1.TextMatrix(Datagrid1.Row, 1)
Text2.Text = Datagrid1.TextMatrix(Datagrid1.Row, 2)
Text3.Text = Datagrid1.TextMatrix(Datagrid1.Row, 3)
'and so on...

If you want it all in one text box, the following -

Text1.Text = Datagrid1.TextMatrix(Datagrid1.Row, 1)
Text1.Text = Text1.Text & VBCRLF & Text1.Text = Datagrid1.TextMatrix(Datagrid1.Row, 2) 'and so on ...

I Want help Of vb6.
What is the code of stope watch.

Kismat, Being a new member and all I can almost understand that you do not know our posting rules, soooo, read them Here . We DO NOT allow the hijacking of other members threads.:)

How about you open your own post and I will gladly help.

Also, show me what YOU have done so far, it might make it easier for me to answer your question (THE NEW ONE) better.;)

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.