2011-07-15 26 views
0

我有一個問題,我找不出來。DataGridViewComboboxCell.Value上的FormatException

我有一個的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"; 

當選項「Klant aanvraag」被選中的用戶得到一個窗口,用戶可以在其中選擇其他客戶。 這是由於用戶未被分配給該客戶的特定項目的原因。 當用戶選擇一個時,它將在Combobox中用以下代碼進行更改。

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

klantindex是需要被選擇的客戶的,因爲它是從組合框中檢索。我認爲這是正確的一種對象。

在此之後,datagridview_dataerror事件引發哪裏出現以下異常文本格式例外。

DataGridViewComboBoxCell值無效。

什麼問題?

回答

0

我認爲這可能是你的值-1。也許你需要從0開始

+0

你幾乎對,uxUrenInvoeren [collumnIndex,row.Index] .value的是-1,但這不是問題。我發佈的方式,它被解決爲答案 –

0

您應該添加所選擇的價值組合框的項集合時,引發異常,因爲分配的值Item集合ComboBoxColumn在沒有找到,因此不是一個有效的值。

嘗試增加其使用Add

(dataGridView1.Columns[0] as DataGridViewComboBoxColumn).Items.Add 
+0

uxInvoerenKlant是dataGridView1.Columns [0] 的名稱,據我所知將取代你的: (dataGridView1.Columns [0] DataGridViewComboBoxColumn)。 我需要comboboxItem,因爲我需要文本和值,對於customerName和customerId –

+0

uxInvoerenKlant將取代0,我認爲你可以將它作爲Add(new ComboBoxItem ...。這些值將基於Text和價值,希望這些ComboBoxItem – V4Vendetta

1

,我發現自己的問題。

uxUrenInvoeren[collumnIndex, row.Index].Value包含ComboBoxItem的價值,而不是ComboBoxItem本身。該代碼現在看起來像這樣:

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

以這種方式進展順利。

感謝您的幫助!

0

解決方案:

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 
+0

使用C#,請用C#在回答這個問題的性質。 –

相關問題