Home Up Feedback Contents Search

ComboGrid 

 

ComboGrid Form

About ComboGrid Data Control

This Combo Grid control behaves like Microsoft Access database grid control, that allows replacement any grid cell by combo box. In Visual Basic, you need to write some code lines and to resolve behaving of few events in order to get the same effect as Microsoft Access database grid control. Following code will help you to do that.

Make your own ComboGrid Data Control

Start with New Project. Default Form1 has been opened. In Project/References make reference to highest version of Microsoft DAO Object Library. Place Data control on the form Form1. Set its DatabaseName property to Biblio.mdb database that is usually in your highest Visual Basic folder. Set its RecordSource property to "Title Author" table. Set second Data control on form Form1. Set its DatabaseName property to Biblio.mdb database, Record Source property to "Titles" table, and Visible property to False. Add Microsoft Data Bound Grid control to Toolbox using Project/Components menu. Select this control from Toolbox and place it on form Form1. Important: DBGrid1 control must be wide enough to catch all row fields without scrolling horizontal scroll bar! Set its DataSource property to Data1 and TabAction property to 1-Column Navigation. Add Microsoft Data Bound List Controls control to Toolbox using Project/Components menu. Select DBCombo control from Toolbox and place it on form Form1. Set its RowSource property to data control Data2, ListField property to "Title" field, DataSource property to Data1, DataField property to "ISBN" field, BoundColumn property to "ISBN" filed, Style property to 2 - vbComboDrop-DownList, and Visible property to False. In Form1 Declaration section add this line:

Also, add this procedure in Declaration section:

Private Sub DisplayCombo()
    DBCombo1.Move DBGrid1.Left + DBGrid1.Columns(0).Left, _
        DBGrid1.Top + DBGrid1.RowTop(DBGrid1.Row), _
        DBGrid1.Columns(0).Width
    DBCombo1.Visible = True
    DBCombo1.SetFocus
End Sub

Add following lines in Form1 Load event:

Private Sub Form_Load()
    Data1.Refresh
    Data2.Refresh
    DBGrid1.RowHeight = DBCombo1.Height
End Sub

Add following lines in DBCombo1 Click event:

Private Sub DBCombo1_Click(Area As Integer)
    DisplayCombo
End Sub

Add following code in DBGrid1 RowColChange event in Scroll event:

Private Sub DBGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)
    If DBGrid1.Col > 0 Then
        DBCombo1.Visible = False
    Else
        DisplayCombo
    End If
End Sub

Private Sub DBGrid1_Scroll(Cancel As Integer)
    DBCombo1.Visible = False
    DBGrid1.Col = 1
End Sub

That is all. Run program. When you click on an ISBN book code field, combo box with corresponding book name is appeared in that field. This is an easier way to edit some field, because user works with record descriptions instead of abstract record indexes.

Also, if you do not want to bother with entering code and control's properties, download already prepared ComboGrid project sample. Note: Don't forget to set DatabaseName property of Data1 and Data2 data controls to your actual Biblio.mdb path!

To make this code completely meets your requirements, you must add application specific code in some control events, such as Data control Validate or Reposition events.

Download area

ComboGrid Project Sample (4KB, ZIP archive)

PK263WSP.EXE (To be used to unzip files downloaded from this site) 

 
Send mail to gaja@cent.co.yu with questions or comments about this web site.
Last modified: April 02, 1999