Click here to Skip to main content
15,909,896 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello
Mr/Ms.

I have one porblem. I used datagridviewcombocolum. Inserting to database is ok. but when select from database, Error message alert me . Error message is following...
'
'
The following Exception is occured in DataGridView:
System.ArgumentException : DataGridComboboxCell value is not valid.
To replace this default dialog please handle the data error event.


my code is following

VB
'This following code is inserting data to datagirdviewcombocolumn
 Public Sub AddToGridComboNC(ByRef _Combo As DataGridViewComboBoxCell, ByVal spName As String, ByVal displaySTR As String, ByVal ValueSTR As String, Optional ByVal dtname As String = "tmp01", Optional ByVal _cAction As String = "3")
        Try
            Dim staff_DataSet As New DataSet
            Dim staff_DataAdapter As New SqlDataAdapter
            m_String = spName & " 0, 0," & _cAction

            Try
                staff_DataAdapter = New SqlDataAdapter(m_String, m_Connection)
                staff_DataAdapter.Fill(staff_DataSet, dtname)
                _Combo.DataSource = staff_DataSet.Tables(0)
                _Combo.DisplayMember = displaySTR
                _Combo.ValueMember = ValueSTR
            Catch ex As Exception
                MessageBox.Show(ex.Message, MessageTitle)
                Err.Clear()
            Finally
                staff_DataSet.Dispose()
                staff_DataAdapter.Dispose()
                m_Connection.Close()
            End Try
        Catch ex As Exception
            MessageBox.Show(ex.Message, MessageTitle)
            Err.Clear()
        End Try
    End Sub

I call above method at dgvpurchase_CellEnter()
VB
AddToGridComboNC(dgvpurchase.Item(4, _CurrentRow), "Storeprocedure_name", "displaymember_name", "value_membername", "MeasurementUnit")

' And following method is selecting data
 Friend Sub RetrieveDataGridView(ByRef _DataGridView As DataGridView, ByVal a As String, ByVal Action As String)
        Dim purchase_dataset As New DataSet
        Dim purchase_dataadapter As New SqlDataAdapter
        Dim cbodatagrid As New DataGridViewComboBoxColumn
        m_String = "SP_SELECT_VENDORPURCHASEDATEIL_20110315_NC '" & a & "',0,0,0," & Action
        purchase_dataadapter = New SqlDataAdapter(m_String, m_Connection)
        purchase_dataadapter.Fill(purchase_dataset, "Item")
        _tmpDT = purchase_dataset.Tables(0)
        Dim i As Integer
                For i = 0 To purchase_dataset.Tables(0).Rows.Count - 1
            With purchase_dataset.Tables(0)
                _DataGridView.Rows.Add()
                _DataGridView.Rows(i).Cells(0).Value = i + 1
                _DataGridView.Rows(i).Cells(1).Value = .Rows(i).Item("productCode").ToString
                _DataGridView.Rows(i).Cells(2).Value = .Rows(i).Item("Description").ToString
                _DataGridView.Rows(i).Cells(3).Value = .Rows(i).Item("weight").ToString
                cbodatagrid.DisplayMember = .Rows(i).Item("MUName").ToString
                Try
                    _DataGridView.Item(4, i).Value = cbodatagrid.DisplayMember ' this is the combo column
                Catch ex As Exception
                End Try
                _DataGridView.Rows(i).Cells(5).Value = .Rows(i).Item("UnitQty").ToString
                _DataGridView.Rows(i).Cells(6).Value = FormatNumber(.Rows(i).Item("vendorpurchaseprice"), 2)
                _DataGridView.Rows(i).Cells(7).Value = FormatNumber(.Rows(i).Item("Amount"), 2)
                _DataGridView.Rows(i).Cells(8).Value = .Rows(i).Item("productID").ToString
            End With
        Next
    End Sub

Please help me as soon as possible ... and please reply to [Email removed to avoid SPAM]
Thanks...
Posted
Updated 17-Mar-11 22:46pm
v2

1 solution

Before assigning you should bind all the MUName in combo box like below.
cbodatagrid.Datasource = dt
cbodatagrid.DisplayMember = "MUName"
cbodatagrid.ValueMember = "MUValue"


Then you can assign the selected value like this,
cbodatagrid.Selecedvalue = .Rows(i).Item("MUValue")
or
cbodatagrid.Text= .Rows(i).Item("MUName").ToString
 
Share this answer
 
Comments
Nyein Chan 18-Mar-11 5:37am    
Thanks for reply solution for my problem
I do follow Toniyo Jackson codes. But not yet ok...
Selectedvalue and Text properties are not include in cbodatagrid....
So how can I do another way.
Toniyo Jackson 18-Mar-11 5:38am    
cbodatagrid.Value = .Rows(i).Item("MUValue")
I hope this will work.
Nyein Chan 18-Mar-11 6:15am    
thanks for reply again..
I doesn't work... inserting data to cbodatagrid is ok.. But I'd like to selected data at _DataGridView.Rows(i).Cells(4).value
_DataGridView.Rows(i).Cells(4) is datagridviewcomboboxcell. I'd like to show data when I saved data at _DataGridView.Rows(i).cells(4). For example..
a,b,c,d datas have in datagridviewcombobox cell
And I enter data at datagrid like following

col1 col2(datagridviewcombo)
asdf b

And I save

when I call asdf data I'd like to show col2 is b
I wrote _DataGridView.Item(4, i).Value = purchase_dataset.Tables(0).Rows(i).Item("MUName").ToString
when assigning data program did not show any error message. but when I hover mouse pointer on combo of datagrid, show Exception Error...

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