Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Dim cmb As New DataGridViewComboBoxCell
    cmb.DisplayMember = "Lastname"
       cmb.ValueMember = "VendorId"

    '''fill dataset using some query having 2 columns lastname and vendorId

    dim m_cnDB as new SqlConnection(connectionString)
    m_cnDB.Open()
    Dim da As New SqlDataAdapter(SQL, m_cnDB)
       Dim ds As New DataSet("Results")
       da.Fill(ds)
    m_cnDB.close()

    ''adding datasource to comboboxCell

    cmb.DataSource = Nothing
       cmb.DataSource = ds.Tables(0)

    ''insert row to datagrid view  3rd column is my combobox column

    DataGridView1.Rows.Insert(DataGridView1.NewRowIndex, New Object() {"1", "xyz", cmb})



I doesn't get error at compile time even while debugging .
but when gridview loads it get exception.

system argument exception: datagridviewcomboboxcell value is not valid

Please help to find out solution

Thank You
Posted
Comments
Sinisa Hajnal 20-Jan-15 2:23am    
It sounds more like data error (note that cell VALUE is not valid). I'd say your VendorId is invalid field name or some such minor oversight.
yogeshysankar 20-Jan-15 2:54am    
Yes it is tell to handle data error event .
Actually i am trying to add datasource to DataGridViewComboBoxCell every time when new row inserted instead to add to DataGridViewComboBoxColumn ,

so is there any trouble of blank row that automatically inserted at the bottom of gridview?
yogeshysankar 20-Jan-15 2:53am    
Yes it is tell to handle data error event .
Actually i am trying to add datasource to DataGridViewComboBoxCell every time when new row inserted instead to add to DataGridViewComboBoxColumn ,

so is there any trouble of blank row that automatically inserted at the bottom of gridview?
Sinisa Hajnal 20-Jan-15 4:45am    
Don't go into the database for each row to fill it. In form load take the data you need and set datasource of the combobox column in RowDataBound Event with vendors.copy method (which will create separate instance of the data source without you having to go into the database - cleaner and MUCH faster.
Sinisa Hajnal 20-Jan-15 4:48am    
I can supply you with the code for setting up combobox column, but from home. So...another 6-10 hours in the future.

1 solution

OK, here is the code for binding combobox column along with other columns.
Note two things:
- binding column name on the combobox column is the same as value member. The names are not neccessarily the same, but values should be. That is, if you're binding status list into combobox, DataPropertyName should reference column that contains status(es) in grid datasource.

- datasource for combobox column and gridview are different (this is obvious, but still to make it clear)

Calling this code from page load - if you don't know your data at that point, you can assign datasource at a later point in time - just bind the columns with column names.


VB
With dgv
               .AutoGenerateColumns = False

               .Columns(1).DataPropertyName = "quantity"

               .Columns(2).DataPropertyName = "value_type"
               With DirectCast(.Columns(2), DataGridViewComboBoxColumn)
                   .DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox
                   .ValueMember = "value_type"
                   .DisplayMember = "value_type_name"
                   .DataSource = _source
               End With
              .Columns(3).DataPropertyName = "unit_price"


               .DataSource = ds.Tables(1)
           End With



If this helps please take time to accept the solution. Thank you.
 
Share this answer
 
v2
Comments
yogeshysankar 21-Jan-15 23:37pm    
hi thanks for your reply ...

i think by using this code , same datasource may be applicable to cells belongs to that column but i want to set different datasources with same value and display member for each cell belongs to that column....

i doesnot try your code ..i will try that and see what will exactly happen...

Thanks

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