Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I select row in a datagrid

Status
Not open for further replies.

Askeladden

Programmer
Jan 28, 2004
87
0
0
NO
How do I select a specific row ( the one I have the arrow pointing to ). I have coded it so that when I double click the datagrid I am taken to a different form, but the topmost record of the datagrid is accessed.
Can someone help me with this problem?
Thank ahead of time.
Christian :)
 
If the grid is bound to a recordset, the .bookmark property of that recordset should point to the current record. I assume that by 'arrow' you refer to the recordselector at the far left of the grod?
 
By grod, I of course mean grid!

You can also set the .Bookmark property to select the record in the grid. This is distinct from the SelBookMarks collection.
 
yes, that is correct, the arrow I refer to is the recordselector. But how would I set the .bookmark property?
 
If the user selects the row via the grid's record selector, the recordset's bookmark property will automatically point to the record they have selected. If you want to set it to a different row it will be automatically set via a .MoveFirst, .MoveNext, .Find etc (ADO assumed).

I'm not 100% clear on what you are trying to achieve here - more detail may be useful. If you are trying to pass details to another form, it may be better to think in terms of passing the unique key for the selected record. If the grid is bound to a recordset called rsMine and the key field is, for example, UserName then you would simply want to pass rsMine!UserName through to the other form. The value passed will be from the 'current' record which should always correspond to the one marked by the record selector in the grid.

Does this answer your question?
 
yes, but how would i code this? Also the field in this spesific record should go to different Textboxes in the other form.
 
SELECT Forfall, Ansvarlig, Eiendom, Bygg, Anlegg, [Ordre Beskrivelse], Merknader, ID FROM [Vedlikeholds Rutiner]

I have populated my datagrid with a recordset with the following fields: Date, Place, and Sale.

I want to select a spesific record from this recorset:

03/08-2000 Oslo Villa
04/03-2001 Madrid Apartment
05/06-2001 Tokyo House

I select item #2 with the recordselector.

Now I, when I doble click on the datagrid or press "go to that record", the program will access a different form, where '04/03-2001' should go into the new forms 'date' textbox and 'Madrid' should go into its 'place' textbox,etc.

I would appriciate some dummy-code to help me.

Thanks ahead of time
Christian
 
Not necessarily the best approach but, if module with grid is FrmGrid and other module is FrmText,

in FrmGrid grid doubleclick event:

Code:
FrmText.RecId = rs!ID

in FrmText
Code:
'in declarations
Public RecId as Long

'in Form_Load perhaps
dim rsLocal as New ADODB.Recordset, SqlTxt as String
SqlTxt = "SELECT * FROM [Vedlikeholds Rutiner] WHERE ID = " & RecId
rsLocal.Open SqlTxt, DbConnect, adOpenForwardOnly, adLockReadOnly, adCmdText
set textbox1.datasource = rsLocal
set textbox2.datasource = rsLocal
textbox1.datafield = "Date"
textbox2.datafield = "Place"
 
Thanks for the info and the code. I tried it yesterday, have been away from terminal, however it do not work when I adapt it for the program. The form (called frmText here) is not the first form to be loaded into the computer, the grid is. And I get an error concerning "rs!ID".
Christian
 
Hi Glasgow,

I just asked a similar question a day or two ago. I got it working by setting a global variable with the datagrid's current record value of the unique ID field of the record. Then requerying the DB with the value of the globalvariable.

>Not necessarily the best approach

What would be the best approach. I plan on using the datagrid quite a bit for searching and selecting records for edit,update,delete and view, and need a solid way to go about this before I implement too much. Any comments would help. TIA
 
Hi guys
U can try this. I am assuming that from the datagrid u r taking one a particular row data to the form 2 text boxes.

Dim f as Form1

If Datagrid1.Recordselector=True Then
Set f.Text1.Text= Datagrid1.Columns(0).Value
Set f.Text2.Text= Datagrid1.Columns(1).Value
End If

I hope this would help..
Jupbs
 
Thanks Jupbs.
I put the code into my program, and ran it. I got the error message: Method or data member not found. Any suggestion on how i can fix this, so that it runs correctly.
Thank ahead of time :)
Christian
 
You can add a boomark in the SelChange event as follows. You must also prevent the user to select multiple records. dgr = your DataGrid. rst = your recordset.
Code:
Private Sub dgr_SelChange(Cancel As Integer)
    dgr.SelBookmarks.Add dgr.Bookmark
        If dgr.SelBookmarks.Count > 1 Then
            Cancel = True
        End If
        If Not rst.RecordCount = 0 Then gr.SelBookmarks.Add dgr.Bookmark
End Sub

To retrieve the bookmark:

Check first whether the user selected a row
If dgr.SelBookmarks.Count >= 1 Then ....

Loop through the bookmarks collection to get the bookmark:

Dim bmk as Variant

For Each bmk in dgr.bookmarks
dgr.Bookmark = bmk
Your code to get the field values for your textboxes....
Next bmk

Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top