VB 6.0 ComboBox in DataGrid


By Madhava Rao Bitra
Printer Friendly Version
  

Here is a quick code sample demonstrating how to incorporate a ComboBox in DataGrid cells and wire up their events.



We can place a combo box inside the data grid control with the help of few event handlings.

 

I am going to give you a step by step description to do it.

 

Step1:

Place a combo box any where in the form and populate the values as you need. Set the visible property to false in design time itself.

 

Step2:

Load the data grid with the required data from the data base using adodc connection.

 

E.g: For the data grid named rptGrid we can load the data as fallows

    ‘ Variable to store the SQL Query

    StrString = “Select * From CustomerMaster_Live Where Purgedata = 0”

             

    Adodc1.RecordSource = SelStr

    Adodc1.Refresh

 

    rptGrid.ClearFields

 

    rptGrid.Refresh

    rptGrid.ReBind

    rptGrid.Refresh

 

Step3:

Now we can set the combo box on any cell of the selected column, let’s say on column 7.

The fallowing code shlould write in the row column change event of the data grid

 

Here  cmbGridDisposition is the name of the requred combo box.

 

If rptGrid.Col = 7 Then

        cmbGridDisposition.Visible = True

        cmbGridDisposition.Top = rptGrid.Columns(7).Top

        rptGrid.RowHeight = cmbGridDisposition.Height

cmbGridDisposition.Top = rptGrid.Columns(7).Top + rptGrid.Top + rptGrid.RowTop(rptGrid.Row)

        cmbGridDisposition.Top = cmbGridDisposition.Top - 200

        cmbGridDisposition.Width = rptGrid.Columns(7).Width

       

        cmbGridDisposition.Left = rptGrid.Columns(7).Left + rptGrid.Left

       

        If Len(rptGrid.Columns(7).Text) > 0 Then

            cmbGridDisposition = rptGrid.Columns(7).Text

        End If

    Else

        cmbGridDisposition.Visible = False

    End If

This will work only if the cell value matched with the list of combo box values.

 

Step4:

To affect the selected value from the drop down over the grid cell. We should write code under the

Click event of the combo box.

 

    If cmbGridDisposition.Visible = True Then

        rptGrid.Columns(7).Text = cmbGridDisposition.Text

    End If

 

By running the code you can able to select value for a particular column from the drop down list.




button

 
Article Discussion: To place a combo box in the data grid control
Madhava Rao Bitra posted at 13-Oct-07 12:17

Original Article

Can you explain in clear?


 
Combo box in data grid control
Debbie Coates replied to Madhava Rao Bitra at 04-Jan-08 07:12

I have adapted and used the example given to successfully place a combo box in my datagrid

however, I have One problem

when I later call a command to update the underlying recordset of the datagrid, I get an error because I am trying to update the many side of a one to many table relationship.

In actual face, i want to display this record on my datagrid, but I dont actually want to update that particular field

this code sets my datagird

If cboFundSource.Visible = True Then
      grd_FundAllocation.Columns(1).Text = cboFundSource.ItemData(cboFundSource.ListIndex)
      grd_FundAllocation.Columns(2).Text = cboFundSource.Text
      grd_FundAllocation.Columns(3).Text = gURN
End If

and when i update it later i use

        With rs
        .MoveFirst
        .UpdateBatch
        End With

as i said, this fails becasue i am trying to update col(2), i actually only want to update columns 1 and 3

I would apprecieate any help in solving this

 

regards