Results 1 to 9 of 9

Thread: error system.argumentexception: datagridviewcomboboxCell value not Valid

  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

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: error system.argumentexception: datagridviewcomboboxCell value not Valid

    Could you post a demo showing the problem?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Re: error system.argumentexception: datagridviewcomboboxCell value not Valid

    Quote Originally Posted by Peter Swinkels View Post
    Could you post a demo showing the problem?
    Hi Peter, I have added a sample of Code, which if you run by adding attached Database, you can see the issue ( you just have to try to change value in last visible column in Datagridview)
    hope it will give you insight

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: error system.argumentexception: datagridviewcomboboxCell value not Valid

    You should follow the CodeBank link my signature below and check out my thread on adding a combo box column to a DataGridView. You need to bind or add items to the column before you bind data to the grid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,945

    Re: error system.argumentexception: datagridviewcomboboxCell value not Valid

    Quote Originally Posted by Keerti View Post
    Hi Peter, I have added a sample of Code, which if you run by adding attached Database, you can see the issue ( you just have to try to change value in last visible column in Datagridview)
    hope it will give you insight
    At the moment I have no way to open your attached file, which by the way, is just a database and the code you provided depends on several things that are missing.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Re: error system.argumentexception: datagridviewcomboboxCell value not Valid

    Quote Originally Posted by jmcilhinney View Post
    You should follow the CodeBank link my signature below and check out my thread on adding a combo box column to a DataGridView. You need to bind or add items to the column before you bind data to the grid.
    Thanks for this reference, I am already using it in my projects. Here My query is Can I use different Tables for comboboxes in different rows?
    In the Example I have added as Code in my First Post Question, It has 2 different set of Items in 2 Rows. And If I change Value in one row combo and then click the Other Rows Combo Column, I get Error.

  7. #7

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Re: error system.argumentexception: datagridviewcomboboxCell value not Valid

    Quote Originally Posted by Peter Swinkels View Post
    At the moment I have no way to open your attached file, which by the way, is just a database and the code you provided depends on several things that are missing.
    I have changed the Code and now you will not need any attachment to run this.
    Please note this is a part of My Project, not the complete project, hence you might find some things illogical, but I hope I have been able to explain my problem.
    "It has 2 different set of Combo Items in 2 Rows. And If I change Value in one row combo and then click the Other Rows Combo Column, I get Error."

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: error system.argumentexception: datagridviewcomboboxCell value not Valid

    Quote Originally Posted by Keerti View Post
    Thanks for this reference, I am already using it in my projects. Here My query is Can I use different Tables for comboboxes in different rows?
    In the Example I have added as Code in my First Post Question, It has 2 different set of Items in 2 Rows. And If I change Value in one row combo and then click the Other Rows Combo Column, I get Error.
    Sorry, I should have read the question more carefully. The answer is yes, but it will be a bit fiddly. If you set the DataSource for the column then that flows through to all cells but you can set each cell's DataSource individually. The problem is that you will need to bind the cells before the grid still, which may be tricky. Possibly the best way would be to create one list for the column that will cover all possibilities, so initial binding works, then create individual lists only when editing a cell. When I can, I'll take a closer look and try to create an example.
    Last edited by jmcilhinney; Oct 15th, 2022 at 08:44 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    New Member
    Join Date
    Jul 2022
    Posts
    15

    Re: error system.argumentexception: datagridviewcomboboxCell value not Valid

    Quote Originally Posted by jmcilhinney View Post
    Sorry, I should have read the question more carefully. The answer is yes, but it will be a bit fiddly. If you set the DataSource for the column then that flows through to all cells but you can set read cell's DataSource individually. The problem is that you will need to bind the cells before the grid still, which may be tricky. Possibly the best way would be to create one list for the column that will cover all possibilities, so initial binding works, then create individual lists only when editing a cell. When I can, I'll take a closer look and try to create an example.
    Thanks, I would appreciate, if you could create any example, ( whenever possible).

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