Results 1 to 9 of 9

Thread: error system.argumentexception: datagridviewcomboboxCell value not Valid

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    error system.argumentexception: datagridviewcomboboxCell value not Valid

    I have a DGV(unbound) with many Columns and one of the Column is ComboBoxColumn. I want to add Items in it whenever user enters in the First column. This I am able to do on CellValueChange Event . i.e. if Cell Value of Index 1 changes then 3related Items (say A,B &C) are added to combo of Comboboxcolumn and item at 0 Index (A) is set as default selection, and before adding these items I am clearing the Combobox.
    Please note that Items to be added in combo may be different in different rows, e.g. maybe in 2nd Row Combo 3related Items may be say A,D &F.
    After adding some data , if User want to EDIT the selection done in Combobox in any row then user will directly enter in that Combobox column. So to take care of it I am taking help of CellBeginEdit Event to Load the 3 Items related to Column1 Item of that Particular Row. Loading is successful.
    Now issue comes if user changes the selection in combobox by directly entering in the combobox Column, it gives error
    “system.argumentexception: datagridviewcomboboxCell value not Valid”
    My Best guess is it due to different Itemset of combo in different rows.
    What may be the solution, any suggestions?


    Code:
    Public Class Test2
             Dim CBItmUnit As New DataGridViewComboBoxColumn
        Const ClmnCntDGV2 As Integer = 5
        Dim UnitArrayList As New ArrayList
        Dim V_SalesVoucher As New DataTable
    
        Private Sub Test2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            With V_SalesVoucher.Columns
                .Add("1", GetType(Integer))
                .Add("2", GetType(String))
                .Add("3", GetType(Integer))
                .Add("4", GetType(String))
                .Add("5", GetType(Integer))
    
            End With
            With V_SalesVoucher.Rows
                .Add(1, "Itm1", 10, "Box", 100)
                .Add(2, "Itm2", 20, "Box", 110)
            End With
    
            With DGVItemDetail
                Try
                    '       Set AutoGenerateColumns False
                    .AutoGenerateColumns = False
                    '       Set Visible Columns Count
                    .ColumnCount = ClmnCntDGV2
                    '       Add Columns
                    .DataSource = V_SalesVoucher
                    ' // Set Column Header Text and Width
                    .RowHeadersVisible = True
    
                    .Columns(0).HeaderText = "S.No." : .Columns(0).Width = 70 : .Columns(0).ReadOnly = True : .Columns(0).DataPropertyName = "1"
                    .Columns(1).HeaderText = "Item" : .Columns(1).AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill : .Columns(1).DataPropertyName = "2"                                '
                    .Columns(2).HeaderText = "Qty" : .Columns(2).Width = 90 : .Columns(2).DataPropertyName = "3"
                    .Columns(3).HeaderText = "List Price" : .Columns(3).Width = 100 : .Columns(3).DataPropertyName = "4"
                    .Columns(4).HeaderText = "Unit" : .Columns(4).Width = 100 : .Columns(4).Visible = False : .Columns(4).DataPropertyName = "5"
    
    
    
                    .Columns.Insert(5, CBItmUnit)
                    CBItmUnitSetting()
    
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
    
                .ReadOnly = False
            End With
    
        End Sub
    
        Private Sub CBItmUnitSetting()
    
            With CBItmUnit
                .Name = "CBItmUnit"
                .DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox
                .DisplayStyleForCurrentCellOnly = True
                .DataSource = UnitArrayList
                .HeaderText = "Unit"
                .Width = 90
            End With
    
            For n As Integer = 0 To DGVItemDetail.Rows.Count - 1
                If DGVItemDetail(4, n).Value IsNot Nothing Then
                    UnitArrayList.Add(DGVItemDetail(4, n).Value)
                    DGVItemDetail(5, n).Value = DGVItemDetail(4, n).Value
                End If
            Next
    
        End Sub
    
        Private Sub DGVItemDetail_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DGVItemDetail.CellBeginEdit
            If DGVItemDetail.CurrentCell.ColumnIndex = 5 Then
                Dim nrow As Integer = DGVItemDetail.CurrentCell.RowIndex
                
                If nrow = 0 Then                                                   'Findrows.Count > 0 Then
                    UnitArrayList.Clear()
                    UnitArrayList.Add("Box")                           '(Findrows(0)("sItmMainUnit").ToString)
                    UnitArrayList.Add("Pc")                                 '(Findrows(0)("sItmAltUnit").ToString)
                    UnitArrayList.Add("Case")                                   '(Findrows(0)("sItmPackUnit").ToString)
                    CBItmUnit.DataSource = UnitArrayList
                    SendKeys.Send("{F4}")
                ElseIf nrow = 1 Then
                    UnitArrayList.Clear()
                    UnitArrayList.Add("Box")                           '(Findrows(0)("sItmMainUnit").ToString)
                    UnitArrayList.Add("Pack")                                 '(Findrows(0)("sItmAltUnit").ToString)
                    UnitArrayList.Add("XXX")                                   '(Findrows(0)("sItmPackUnit").ToString)
                    CBItmUnit.DataSource = UnitArrayList
                    SendKeys.Send("{F4}")
                End If
    
            End If
        End Sub
    
    
    
    End Class
    Last edited by Keerti; Oct 15th, 2022 at 04:20 AM. Reason: Editing the Code Sample

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width