|
-
Jan 16th, 2010, 10:33 AM
#1
Thread Starter
Addicted Member
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
-
Jan 16th, 2010, 11:01 AM
#2
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:
myBindingSource.DataSource = myDataTable myDataGridView.DataSource = myBindingSource firstNameTextBox.DataBindings.Add("Text", myBindingSource, "FirstName") lastNameTextBox.DataBindings.Add("Text", myBindingSource, "LastName")
-
Jan 16th, 2010, 11:05 AM
#3
Thread Starter
Addicted Member
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
-
Jan 16th, 2010, 11:13 AM
#4
Thread Starter
Addicted Member
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
-
Jan 16th, 2010, 11:28 AM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|