You are currently offline, waiting for your internet to reconnect

How to highlight a specific row in the DataGrid control 6.0

This article was previously published under Q195472
This article describes how to programmatically highlight a specific row in the DataGrid control (6.0).

The same procedure can be applied in Visual Basic 5.0 using the DBGridControl 5.0.
In order to highlight a specific row in the DataGrid you need to specifythe bookmark of the target record within the recordset of your data source.You need to add this bookmark to the collection of bookmarks.

Each bookmark previously appended to the collection shows in the range ofselected rows on your DataGrid control. You need to explicitly remove theserows from the collection before appending the bookmark for the record youwish to highlight.

The following Visual Basic 6.0 sample code demonstrates the process ofsearching for a record and then highlighting the hit within your grid:
  1. Start a new Standard EXE project in Visual Basic. Form1 is created by default.
  2. Select Components on the Project menu. Select "Microsoft DataGrid Control 6.0 (OLE DB)." Select Project References and add "Microsoft ActiveX Data Objects 2.x Library."
  3. Add a CommandButton to your form; the default is Command1. Add a DataGrid control to your form; the default is DataGrid1.
  4. Place the following code in the General Declaration section of Form1:
          Option Explicit      Dim cn As New ADODB.Connection      Dim rs As New ADODB.Recordset      Dim cmd As New ADODB.Command      Dim i As Integer      Dim FindLastName As String      Private Sub Command1_Click()         ' Remove previously saved bookmark from collection         If (DataGrid1.SelBookmarks.Count <> 0) Then            DataGrid1.SelBookmarks.Remove 0         End If         ' Prompt user for desired author's last name         FindLastName = InputBox("Please enter the author's last name you                                  want to search for", "Find")         rs.Find "au_lname = '" & FindLastName & "'", , , 1         ' Append your bookmark to the collection of selected rows         DataGrid1.SelBookmarks.Add rs.Bookmark      End Sub      Private Sub Form_Load()         ' Open your ADO connection where "Pubs" is an ODBC DSN that         ' points to pubs database in SQL Server         cn.Open "Pubs"         ' Create your command to query for all records in Authors table         With cmd            .ActiveConnection = cn            .CommandText = "select * from authors"         End With         ' Open your recordset         With rs            ' Set rs properties            .CursorType = adOpenKeyset            .LockType = adLockOptimistic            ' Call open using active command            .Open cmd         End With         ' Populate the DataGrid providing rs as the data source         Set DataGrid1.DataSource = rs      End Sub
  5. Press the F5 key to run the application. Click the CommandButton and enter an Author's last name. Note it moves the cursor to the Author selected and highlights the entire row.

Article ID: 195472 - Last Review: 08/03/2005 17:51:33 - Revision: 2.1

Microsoft Visual Basic Enterprise Edition for Windows 6.0

  • kbconfig kbctrl kbhowto kbsetup KB195472