Combobox in an ADO datagrid

Posted on 2004-11-27
Last Modified: 2012-06-21

I am a VB6 beginner, and I am looking to use an ADO datagrid with a combobox.  Basically I want to put the combobox on the current datagrid cell so that I can update the grid with a selection from the combo.  The problem I have is this:

I can do it OK with a flexgrid, as follows:

Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
Combo1.Visible = False
        Combo1.Left = MSFlexGrid1.Left + MSFlexGrid1.CellLeft
        Combo1.Top = MSFlexGrid1.Top + MSFlexGrid1.CellTop
        Combo1.Width = MSFlexGrid1.CellWidth
        Combo1.Visible = True
End Sub

but I want to use a datagrid because a databound flexgrid is only readonly, and I want to be able to update my database table automatically.  This I can do with a datagrid.  But I can find no way of referencing the datagrid cell in the necessary way.  Help!  Is this impossible with a datagrid???

Alternatively, does anyone know a freeware grid with the above capabilities (ie databinding and cell referencing).

Many thanks,
Question by:trimtrom
    1 Comment
    LVL 69

    Accepted Solution

    Add a ListBox and make its visible property to False.

    'Add this code to the form declaration

    Option Explicit
    Dim intColIdx As Integer
     'This will contain the index for the current  cell in the dbgrid
    Dim blnListShow As Boolean 'is the list showing or not

    Private Sub DBGrid1_ButtonClick(ByVal ColIndex As Integer)
    Dim intTop As Integer
    intColIdx = ColIndex
    If blnListShow = False Then 'if the list is not showing then...
    blnListShow = True
    List1.Left = DBGrid1.Columns(ColIndex).Left + 360
    intTop = DBGrid1.Top + (DBGrid1.RowHeight * (DBGrid1.Row +2))
     List1.Top = intTop
     List1.Width = DBGrid1.Columns(ColIndex).Width + 15
                         List1.Visible = True
    Else 'if the list is show, hide it
                 blnListShow = False
                  List1.Visible = False
    End If
    End Sub

    Private Sub Form_Load()
       blnListShow = False 'initialize
    End Sub

    Private Sub List1_Click()
       DBGrid1.Columns(intColIdx).Text = List1.Text
       'set the value of the dbgrid
       List1.Visible = False 'hide the list
    End Sub

    Private Sub List1_LostFocus()
       blnListShow = False
       List1.Visible = False
    End Sub  

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now