Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
hello,

I am trying to display a databound combobox in a Datagridview based on the selection in a mother combobox which is out of the said grid.
I am able to display the blank combobox column in grid but gets the above error when the data is to be displayed.
VB
Private Sub GridDesign()

        'TODO: TO CREATE A COMBO BOX IN GRID TO ALLOW SELECTION OF GROUP UNDER BY NAME FOR ACCOUNT CREATION

        Dim ddlGroupUnderingrid As New DataGridViewComboBoxColumn()
        
	ddlGroupUnderingrid.DataPropertyName = "ddlGroupUnderingrid"
        ddlGroupUnderingrid.HeaderText = "Under Group"
        ddlGroupUnderingrid.Name = "ddlGroupUnderingrid"
        DataGridView1.Columns.Add(ddlGroupUnderingrid)
        

        DataGridView1.Columns.Add("FAAMC", "Account Code")
        DataGridView1.Columns.Add("FAAMN", "Account Name")
        DataGridView1.Columns.Add("FAAMOB", "Opening Bal.")

        
        DataGridView1.Columns(0).Width = 200
        DataGridView1.Columns(2).Width = 200


    End Sub



Private sub selected index change

    Dim DA As SqlDataAdapter = New SqlDataAdapter
              DA.SelectCommand = New SqlCommand("Select FAAM.FAAMU,FAGR.FAGRN, FAAM.FAAMC, FAAM.FAAMN, FAAM.FAAMOB from FAAM " +
                                          "INNER JOIN FAGR ON FAAM.FAAMU = FAGR.FAGRC WHERE FAGR.FAGRC=@FAGRC", connection)

        DA.SelectCommand.Parameters.Add(New SqlParameter("@FAGRC", (ddlUnderGroup.SelectedValue)))


        Dim datatable As New DataTable
        DA.Fill(datatable)

        DataGridView1.AutoGenerateColumns = False
        DataGridView1.DataSource = datatable

        For Each row As DataGridViewRow In DataGridView1.Rows
            Try

                DataGridView1.Columns(0).DataPropertyName = "FAGRN"
                DataGridView1.Columns(1).DataPropertyName = "FAAMC"
                DataGridView1.Columns(2).DataPropertyName = "FAAMN"
                DataGridView1.Columns(3).DataPropertyName = "FAAMOB"

            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
Next

End Sub
Posted
Updated 6-Feb-14 1:09am
v2

Instead of DataGridView1.Columns(0).DataPropertyName = "FAGRN"
you probably need to set the DisplayMember and ValueMember properties since this is a drop down type column.
 
Share this answer
 
Comments
atul sharma 5126 6-Feb-14 9:29am    
Tried it but still same error
atul sharma 5126 6-Feb-14 9:37am    
the error is now removed by removing the property also from grid design. But the combobox is showing blank. thanks in advance for the support
GOT IT.

I have to provide the datasource again for the combobox.

well i don't understand why because i have provided the same for datagrid. but anyway the data is now populated in combobox.

BUT now a new issue:
By default the combo box is showing blank and i have to click on the arrow button to populate.
How can i show the data by default?
any ideas?
 
Share this answer
 
SQL
datagrid1.DataSource = datatable1
            For index As Integer = 0 To datatable1
                Dim Cmb As New DataGridViewComboBoxCell()
                Cmb = CType(datagrid1.Rows(index).Cells("fieldname"), DataGridViewComboBoxCell)
                Cmb.Value = datatable1.Rows(index)("PartClaimType").ToString()
            Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900