0

I have a problem which I can't figure out.

I have an DataGridViewComboboxCell,

List<ComboBoxItem> klanten = new List<ComboBoxItem>();
foreach (ICustomer customer in CustomerFactory.CreateCustomers())
{
     klanten.Add(new ComboBoxItem(customer.Naam, customer.Id));
}
klanten.Add(new ComboBoxItem("Klant aanvraag", -1));

uxInvoerenKlant.DataSource = klanten;
uxInvoerenKlant.DisplayMember = "Text";
uxInvoerenKlant.ValueMember = "Value";

When the option "Klant aanvraag" is selected the user gets a window where the user can choose another customer. This is for the reason the user was not assigned for a specific project for that customer. When the user chose one it will be changed in the Combobox with the following code.

uxUrenInvoeren[collumnIndex, row.Index].Value = uxInvoerenKlant.Items[klantIndex];

klantindex is the customer that needs to be selected, because it is retrieved from the combobox. It is the right kind of object in my opinion.

After this, the datagridview_dataerror event is raised where I get the Format exception with the following exception text.

DataGridViewComboBoxCell value is not valid.

What is the problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lukas Huzen
  • 183
  • 1
  • 10

4 Answers4

1

I found the problem myself.

The uxUrenInvoeren[collumnIndex, row.Index].Value contained the value of the ComboBoxItem and not the ComboBoxItem itself. The code now looks like this:

ComboBoxItem item = uxInvoerenKlant.Items[klantIndex] as ComboBoxItem;
if (item != null)
{
    uxUrenInvoeren[collumnIndex, row.Index].Value = item.Value;
}

In this way it goes well.

Thanks for the help!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lukas Huzen
  • 183
  • 1
  • 10
0

I think it may be your value of -1. Maybe you need to start with 0

saj
  • 4,626
  • 2
  • 26
  • 25
  • you were almost right, the uxUrenInvoeren[collumnIndex, row.Index].Value was -1 but this was not the problem. i posted the way it is solved as an answer – Lukas Huzen Jul 15 '11 at 11:37
0

You should add the selected value to the items collection of the combobox, the exception is raised since the value assigned is not found in the Item collection of the ComboBoxColumn and hence not a valid value.

Try adding it using Add

(dataGridView1.Columns[0] as DataGridViewComboBoxColumn).Items.Add
V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
  • uxInvoerenKlant is the name of dataGridView1.Columns[0] as far as i know would that replace your: (dataGridView1.Columns[0] as DataGridViewComboBoxColumn). and i need the comboboxItem because i need text and value, for customerName and customerId – Lukas Huzen Jul 15 '11 at 11:10
  • uxInvoerenKlant would replace 0 and i think you can do it as Add(new ComboBoxItem... . The values would be controlled on the basis of Text and Value, hope these are properties of ComboBoxItem – V4Vendetta Jul 15 '11 at 11:14
-1

Solution :

Private Sub gvPrint_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles gvPrint.DataError
    If e.Context = DataGridViewDataErrorContexts.Formatting Or e.Context = DataGridViewDataErrorContexts.PreferredSize Then
        e.ThrowException = False
    End If
End Sub
Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
dipan chikani
  • 199
  • 4
  • 13