p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/forumdisplay.php?f=76)
-   -   populating a text field (http://p2p.wrox.com/showthread.php?t=14381)

sand133 June 18th, 2004 07:22 AM

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?


jaucourt June 18th, 2004 07:38 AM

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


sand133 June 18th, 2004 07:41 AM

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


sand133 June 18th, 2004 07:54 AM

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


jaucourt June 18th, 2004 08:02 AM

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


sand133 June 18th, 2004 08:16 AM

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


jaucourt June 18th, 2004 08:21 AM

Are you using a dataset for your data source?


sand133 June 18th, 2004 08:24 AM

yes a dataset called dataset1


jaucourt June 18th, 2004 08:30 AM

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)




sand133 June 18th, 2004 08:38 AM

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



All times are GMT -4. The time now is 03:33 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.