Results 1 to 5 of 5

Thread: Datagrid to Textbox

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Datagrid to Textbox

    Is it possible to transfer the contents of a row in a datagrid to a textbox? (Like, when I click row 3, the contents of that row will be transferred to the textboxes) If it is, can anyone guide me to a tutorial? Or is this technique applicable only to the ListView object?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

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

    Re: Datagrid to Textbox

    Text is text. It doesn't matter where it comes from. You can get text from any control and display it in another control.

    That said, displaying data in both a DataGridView and TextBoxes is easier than it is a ListView and TextBoxes because the ListView doesn't support data-binding. With a ListView you would have to copy the data manually. With a DataGridView you simply bind the same data to both the grid and the TextBoxes and everything else happens automatically, e.g.
    vb.net Code:
    1. myBindingSource.DataSource = myDataTable
    2.  
    3. myDataGridView.DataSource = myBindingSource
    4.  
    5. firstNameTextBox.DataBindings.Add("Text", myBindingSource, "FirstName")
    6. lastNameTextBox.DataBindings.Add("Text", myBindingSource, "LastName")
    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
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Datagrid to Textbox

    I'm a bit lost. If let's say my datasource is the datagrid itself, which, when the form loads, is filled by data from the database, would this code:

    Code:
          myBindingSource.DataSource = myDataTable
          myDataGridView.DataSource = myBindingSource
          firstNameTextBox.DataBindings.Add("Text", myBindingSource, "FirstName")
          lastNameTextBox.DataBindings.Add("Text", myBindingSource, "LastName")
    be something like this (this is how I filled the datagrid with data):

    Code:
    dgBook.DataSource = dbDsetTransact
    dgBook.DataMember = "Book"
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  4. #4

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Datagrid to Textbox

    Um, I'm sort of looking for something like this:

    http://www.vbforums.com/showthread.php?t=493902

    The thing is, the code that was posted in that thread has some problems in it.

    eg:

    Code:
           txtTitle.Text = dgBook.Item(1, e.RowIndex).Value
    Only transfers the contents of the currently selected row of the dgBook when I click on the 2nd column. Does it have something to do with the event arguments?

    Code:
    Private Sub dgBook_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgBook.CellContentClick
    ====================
    ほんとにどもありがとう!

    Rie Ishida

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

    Re: Datagrid to Textbox

    This:
    Code:
    dgBook.DataSource = dbDsetTransact
    dgBook.DataMember = "Book"
    is functionally equivalent to this:
    Code:
    dgBook.DataSource = dbDsetTransact.Tables("Book")
    which is functionally equivalent to this:
    Code:
    Dim myDataTable As DataTable = dbDsetTransact.Tables("Book")
    dgBook.DataSource = myDataTable
    My original code puts a BindingSource in there too. No big deal.
    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

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