 |
| VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 18th, 2004, 07:22 AM
|
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
populating a text field
hi guys im trying to populate some text fields by double clicking on a row in my data grid.
any ideas?
|
|

June 18th, 2004, 07:38 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
In the Datagrid.DoubleClick event, you could use the CurrentCell property, if you wanted the contents of the currently selected cell to be copied into a textbox:
Code:
Private Sub DataGrid1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.DoubleClick
Dim cell As DataGridCell = DataGrid1.CurrentCell
Dim text As String = Dataset1.Rows(cell.RowNumber)(cell.ColumnNumber).ToString
textbox1.text = text
End Sub
This might need a bit of tweaking as I haven't tested it, but should work
|
|

June 18th, 2004, 07:41 AM
|
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks for that but what if i had more then 1 text box, i mean in my situation i have 8 text boxes that need to filled up by the 8 columns in the data grid
|
|

June 18th, 2004, 07:54 AM
|
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
just tryied tht code out u gave me everything looks good, but i get a error datasetname.Rows, it tells me that Rows is not a member of system.data.dataset
|
|

June 18th, 2004, 08:02 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
I presume you're using a dataset to populate your datagrid? You have to refer to your dataset where I've put Dataset1
If you are using a dataset, you do the following (you also need to know what table you're displaying in the datagrid; the name of this table goes in instead of {MyTable}):
Dim cell As DataGridCell = DataGrid1.CurrentCell
Dim ds As Dataset = Datagrid1.DataSource
' get a handle on the row just clicked on...
Dim dr As Datarow = ds.Tables("{MyTable}").Rows(cell.RowNumber)
' populate text boxes...
textbox1.text = dr(0)
textbox2.text = dr(1)
textbox3.text = dr(2) ' and so ad nauseum...
As before, all this goes in your double click event handler
|
|

June 18th, 2004, 08:16 AM
|
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ok just tried out ure last code, i get a error when i run the program it says 'specified cast is not valid', when i debug it, its refering to ure line in the code where it says Dim ds As Dataset = Datagrid1.DataSource.
i presume ds stands for the name of my dataset
|
|

June 18th, 2004, 08:21 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Are you using a dataset for your data source?
|
|

June 18th, 2004, 08:24 AM
|
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
yes a dataset called dataset1
|
|

June 18th, 2004, 08:30 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
ds doesn't stand for the name of your dataset - it was a new dataset I was defining.
Instead of using ds in my example, use your dataset. Don't bother with the Dim ds as Dataset = datagrid1.datasource at all. To make it work, you'd need to do Dim ds as Dataset = CType(datagrid1.datasource, Dataset)
|
|

June 18th, 2004, 08:38 AM
|
|
Authorized User
|
|
Join Date: Jun 2004
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ok ive just got it working but it only gets the first cell information of the selected row, ie, in my row i have
name, surname and address. the following code only gets name into my text field. I have 3 text fields set up.
Dim cell As DataGridCell = DataGrid1.CurrentCell
Dim text As String = Dataset1.tables(''employee'').Rows(cell.RowNumber) (cell.ColumnNumber).ToString
textbox1.text = text
textbox2.text = text
textbox3.text = text
|
|
 |