0

I have a quite some experience with DataGridViews, I cannot resolve this super simple issue...

I have a datagridview with a comboboxcolumn pre-set possible options (3 simple options). I have a database with table, form which I want to select the correct value for the cell in the column. I need to preserve the option for that the user might change the option later (and save it back to DB).

I created a datatable with options:

    Dim dtShift As New DataTable
    dtShift.Columns.Add("ValueM")
    dtShift.Columns.Add("DisplayM")
    dtShift.Rows.Add({123, "123"})
    dtShift.Rows.Add({345, "345"})
    dtShift.Rows.Add({678, "678"})

Then I assign the values this way (ds is dataset filled with data):

    Me.DataGridView1.AutoGenerateColumns = False
    Dim colpon As DataGridViewComboBoxColumn = Me.DataGridView1.Columns("PonLen")
    With colpon
        .DataSource = dtShift
        .ValueMember = "ValueM"
        .DisplayMember = "DisplayM"
        .DataPropertyName = "PonLen".ToString   ' contains an option like "345"
        '.ValueMember = "PonLen"    ' not needed, if Display member is saved 
    End With

...but this doesn't work well, even though it is as close as I could get. I The columns have correct options, there are correct values selected, but DataErrorEvent floods a test-log TextBox with errors like this:

Row=12 cell=3 : System.FormatException: DataGridViewComboBoxCell value is not valid.
Oak_3260548
  • 1,882
  • 3
  • 23
  • 41
  • 1
    What is the type of column `PonLen`? Make sure that type that column is same as type of `ValueM` column in the option DataTable – Fabio Oct 27 '15 at 06:21
  • PonLen is TinyInt. I'll see how I can specify data type for the datatable columns. – Oak_3260548 Oct 27 '15 at 09:58
  • I specified data type of the value member: dtShift.Columns(0).DataType = System.Type.GetType("System.Int32") - but it didn't help to resolve this issue... – Oak_3260548 Oct 27 '15 at 10:38

1 Answers1

0

OK, so the problem was in the data types, I didn't realized, that a differences like System.Int16 vs System.Byte (ShortInt vs tinyInt) does matter.

DB field: TinyInt (0 to 256)

ValueMember: System.Byte (0 to 256 - changed from System.Int16 or 32)

...and now it works - the same way as it did, but throws no errors :-)

Regards,

Oak

Oak_3260548
  • 1,882
  • 3
  • 23
  • 41