HOWTO: Determine the Contents of a Cell in a DBGrid Control

Last reviewed: September 25, 1997
Article ID: Q149094
The information in this article applies to:
  • Microsoft Visual Basic Control Creation, Learning, Professional, and Enterprise Editions for Windows, version 5.0
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SUMMARY

This article shows how to determine the contents of a selected cell in a DBGrid control.

MORE INFORMATION

The DBGrid control allows you to display the contents of a recordset or a table. With this method, you can take the data from a cell in the MouseUp event of the DBGrid control.

This example uses a DBGrid control bound to Data control. The Data control uses the BIBLIO.MDB database for information. In the MouseUp event of the DBGrid control, the column and row positions of the cell are assigned to an integer variable. A bookmark is then set using the row position. Executing the CellValue method on the Columns collection of the DBGrid control determines the contents of the cell. Those contents then appear on a TextBox control.

Sample Code

  1. Start Visual Basic. If it is already running, on the File menu, click New Project. Add a DBGrid, TextBox, and a Data control to the Form1 form. Set the following controls to the specified properties:

    a. Data control

          DatabaseName property: <path> BIBLIO.MDB
          RecordSource property: Titles
    

    b. DBGrid control DataSource property to Data1.

  2. Copy the following code sample to the Form1 code window:

          Option Explicit
          Dim igColumn As Integer
          Dim igRow As Integer
          Dim vargBookmark As Variant
    

          Private Sub DBGrid1_MouseUp(Button As Integer, Shift As Integer, _
    
                                      X As Single, Y As Single)
             igColumn = DBGrid1.ColContaining(X)   'Set the Cell Column number
             igRow = DBGrid1.RowContaining(Y)      'Set the Cell Row number
       
             vargBookmark = DBGrid1.RowBookmark(igRow) 'Set Bookmark Value
       
             'Show the contents of the cell in a textbox
             Text1.Text = DBGrid1.Columns(igColumn).CellValue(vargBookmark)
       
          End Sub
    
    

  3. On the Run menu, click Start or press the F5 key. Click on a cell and note that the contents is shown in the TextBox.
Keywords          : PrgCtrlsCus VB4ALL VB4WIN vb5all vb5howto
Version           : WINDOWS:4.0 5.0
Platform          : NT WINDOWS
Issue type        : kbhowto


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 25, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.