CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: grids and ODBC

  1. #1
    Join Date
    May 2001
    Posts
    6

    grids and ODBC

    Hi.

    I'm trying to connect a data control to a dbGrid. The data control .Connect property must be set to the proper connection string. This is no problem, except that I don't want to hard-code the UID/PWD into the property. I thought that I could just set the .Connect property at run-time, but this doesn't work--no data shows up in the dbGrid.

    Alternatively, I could use an MSFlexGrid and fill it using a recordset. However, I don't know enough about MSFlexGrids to know if they will have all the features I need. I don't need to edit data, but I need to search, highlight, and select rows.

    Thanks!
    secrethistory




  2. #2
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: grids and ODBC

    Better use MSFlexGrid. You will escape a lot of headache later.
    Here is the code

    If you use an MSFlexGrid control to display data returned in an ADO recordset, you can use this code to
    dynamically populate the grid-including the header row-with the information in the recordset. You need an
    open ADO recordset named rst and a form containing an MSFlexGrid control named msfGrid:

    Dim cln As Field
    With msfGrid
    .Rows = 2
    .Cols = rst.Fields.Count
    'get the number of grid cols
    .FixedRows = 1
    .FixedCols = 0
    .Row = 0
    .Col = 0
    For Each cln In rst.Fields
    .Text = cln.Name
    'populate header row with names of fields
    If .Col < .Cols - 1 Then .Col = .Col + 1
    Next
    Do While Not rst.EOF
    'loop thru recordset to populate grid
    .Row = rst.AbsolutePosition
    'move to the next row
    .Col = 0
    'reset ourselves back to column(0)
    For Each cln In rst.Fields
    If Not IsNull(cln.Value) Then
    .Text = Trim(CStr(cln.Value))
    Else
    .Text = ""
    End If
    If .Col < .Cols - 1 Then .Col = .Col + 1
    Next
    rst.MoveNext
    .Rows = .Rows + 1
    'add a new row to the grid
    Loop
    .Rows = .Rows - 1
    'remove the last row because it's blank
    .Row = 0
    End With


    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

  3. #3
    Join Date
    May 2001
    Posts
    6

    Re: grids and ODBC

    So you can highlight and select fields in MSFlexgrids?

    I am having problems with the code for some reason. I get my recordset with no problem--I can even display some of its data in a message box. But I am getting "Type mismatch" error on the line "For Each cln In rst.Fields". This should work because I can even find it in my textbook. Any ideas?

    Thanks,
    secrethistory


  4. #4
    Join Date
    May 2001
    Posts
    6

    Re: grids and ODBC

    I also discovered that we can't use the .AbsolutePosition property on this type of recordset. I'm using ODBC with a Sybase database.


  5. #5
    Join Date
    May 2001
    Posts
    6

    Re: grids and ODBC

    OK. The MSFlexgrid is not going to work. I got it going, but it took 2 minutes or more to load. Does anyone have any suggestions on setting the .Connect property on the data control at run-time?


  6. #6
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: grids and ODBC

    How many records do you have in your recordset. If you have huge amount of records it will take some time to download. Here you can open not the whole recordset but 1 page at a time.

    Iouri Boutchkine
    [email protected]
    Iouri Boutchkine
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured