Results 1 to 5 of 5

Thread: [RESOLVED] Datagrid-on row click, load text from each cell on the row to textboxes?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    80

    Resolved [RESOLVED] Datagrid-on row click, load text from each cell on the row to textboxes?

    Hi,

    What im trying to do is, when I click on any row on my datagrid(dgvCompositions), all the cells(16 columns excluding ID column) in that row must be loaded into the 16 textboxes I have (txtB1, txtB2, txtB3....)

    So when I click the row, and the entire row is highlighted the text from the cells get loaded into the textboxes.

    I know how to pull data or text from specific cells clicked

    Code:
    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
    
    TextBox1.Text = DataGridView1.Item(0, e.RowIndex).Value
    End Sub
    But the problem I have is that how do I pull all 16 simultaneously and put them into 16 individual text boxes?

    Any help is greatly appreciated
    thx

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagrid-on row click, load text from each cell on the row to textboxes?

    Is your grid bound to a DataTable or some other list? If so then you can bind the same list to the TextBoxes and what you're asking for will happen automatically. Otherwise, yes you have to write a line of code to transfer the data from each cell to each TextBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    80

    Re: Datagrid-on row click, load text from each cell on the row to textboxes?

    my datagrid gets the data from a mysql database

    this is the code im using

    i have a public function to return the connection string

    Code:
    'btnTestButton.....
    Using conn As MySqlConnection = ReturnMyConn()
                Dim strSQL As String = "SELECT * FROM `testtable`"
                Dim da As New MySqlDataAdapter(strSQL, conn)
                Dim dt As New DataTable
                da.Fill(dt)
                dgvCompositions.DataSource = dt
            End Using
    I still dont understand how to create like a On Row Click event to load all the cells to the textboxes

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Datagrid-on row click, load text from each cell on the row to textboxes?

    As I said, you don't have to if your data is bound. You simply bind the data to the TextBoxes too and they will be updated automatically when you select a row in the grid. Not only that, the grid will update automatically with any changes you make in the TextBoxes.

    You'll bind the TextBoxes something like this:
    vb.net Code:
    1. dgvCompositions.DataSource = dt
    2. TextBox1.DataBindings.Add("Text", dt, "ColumnNameHere")
    You'll need one of those lines for each TextBox and you will insert the appropriate column name for each one.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    80

    Re: Datagrid-on row click, load text from each cell on the row to textboxes?

    your solution works perfectly, just tested it out now.

    I did have one problem were when i was switching tables in the same datagrid I had a binding error something about being binded to another source, but I just fixed it by clearing the bindings.

    Thanx! much appreciated!

    damn cant give you any rep soz, need to spread it around lmao

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width