0

我有一个我无法弄清楚的问题。

我有一个 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. 这是因为没有为该客户分配特定项目的用户。当用户选择一个时,它将在 Combobox 中使用以下代码进行更改。

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

klantindex是需要选择的客户,因为它是从组合框中检索的。在我看来,这是正确的对象。

在此之后,datagridview_dataerror在我得到带有以下异常文本的 Format 异常的地方引发了事件。

DataGridViewComboBoxCell 值无效。

问题是什么?

4

4 回答 4

1

我自己发现了问题。

包含 ComboBoxItem的uxUrenInvoeren[collumnIndex, row.Index].Value值,而不是 ComboBoxItem 本身。代码现在如下所示:

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

这样就顺利了。

谢谢您的帮助!

于 2011-07-15T11:36:00.077 回答
0

Item您应该将所选值添加到组合框的项目集合中,引发异常,因为在集合中找不到分配的值ComboBoxColumn,因此不是有效值。

尝试使用添加它Add

(dataGridView1.Columns[0] as DataGridViewComboBoxColumn).Items.Add
于 2011-07-15T10:28:19.053 回答
0

我认为这可能是您的值-1。也许你需要从 0 开始

于 2011-07-15T10:16:53.833 回答
-1

解决方案 :

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
于 2012-09-05T13:14:32.467 回答