Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDisplay data in a DataGrid control and see what cell is selected
KeywordsDataGrid, cell, select
CategoriesControls
 
The following code shows how to bind a DataGrid control to an ADODC (ADO Data Control). First, select the Project menu's Components command. Find the Microsoft ADO Data Control and the Microsoft DataGrid Control entries, and select them.

Now set the ADODC's ConnectString and RecordSource properties to connect the ADODC to the database. Set the DataGrid's DataSource property to the ADODC.

 
Public Sub LoadData()
Dim database_file As String

    ' Get the database path.
    database_file = App.Path
    If Right$(database_file, 1) <> "\" Then database_file = _
        database_file & "\"
    database_file = database_file & "books.mdb"

    ' Connect the ADODC to the database.
    adodcBooks.ConnectionString = _
        "PROVIDER=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & database_file & ";"
    adodcBooks.RecordSource = "SELECT * FROM Books ORDER BY " & _
        "Title"

    ' Bind the ADODC to the DataGrid.
    Set grdBooks.DataSource = adodcBooks
End Sub
 
When the user clicks the Select button, the program uses the DataGrid's Row and Col properties to see what cell was selected. This program just displays the cell's position and value. A more useful program would do something with the data.
 
Private Sub cmdSelect_Click()
    ' Display the current cell's value.
    ' A real applicaiton would do something with it.
    MsgBox "Cell(" & grdBooks.Row & ", " & grdBooks.Col & _
        ") " & vbCrLf & grdBooks.Text
    grdBooks.SetFocus
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated