我有一個未系結的datagridview,將選項設定為允許用戶將行添加為True,我想要的是一旦我“完成”一行,datagridview 會自動創建一個新的空行,所以我設定了方法
DataGridView1.Rows.Add()
在 DataGridView1_Leave 事件中,但是一旦行完成并出現例外: System.Windows.Forms.dll 中發生“System.InvalidOperationException”型別的未處理例外 - 無法在此事件處理程式中完成此操作是什么意思?它應該是簡單的代碼,但我不知道如何解決
情況如下: 那是接收方DataGridView。當我在第一個空白行(由 AllowUserToAddRow = true 創建)并按 F3 時,將打開一個模式表單,其中包含要選擇的產品的另一個 DataGridView

這是接收方 DataGridView 中 KeyDown 事件的代碼
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.F3 Then
If frmZoomArticoli.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim codart As String = frmZoomArticoli.DataGridView1.CurrentCell.Value.ToString()
RicercaxCodiceArticolo(codart)
End If
End If
End Sub

這里的子 RicercaxCodiceArticolo(codart) 用所選產品填充收件人 DataGridView:
Private Sub RicercaxCodiceArticolo(ByVal codiceart As String)
Dim strsql As String
Dim cmd As SqlCommand
Dim source As New BindingSource
'Dichiariamo le variabili che ospitano i dati di riga
Dim codice As String
Dim descrizione As String
Dim unitamisura As String
Dim quantita As Double = 1.0
Dim codiceiva As Double
Dim costobase As Double
Dim prezzobase As Double
Dim costoultimo As Double
Dim giacenza As Double
Dim sconto1 As Double
Dim sconto2 As Double
connection.Open()
strsql = "SELECT CODICEARTICOLO AS 'Codice', DESCRIZIONEARTICOLO AS 'Descrizione', UNITAMISURA AS 'Um', CODICEIVA AS 'Iva' " _
& ", COSTOBASE AS 'Costo', PREZZOBASE AS 'Prezzo', SCONTO1 As 'Sc1', SCONTO2 As 'Sc2', COSTOULTIMO AS 'CostoUlt' " _
& ", BARCODE AS 'Barcode', NOTEARTICOLO AS 'Note' ,CATEGORIAARTICOLO AS 'Categ.Art.', GIACENZA AS 'Giacenza' " _
& ", FORNITOREPREF AS 'Fornit. Pref.' FROM Articoli " _
& " WHERE CODICEARTICOLO = '" & codiceart & "'"
cmd = New SqlCommand()
cmd.CommandText = strsql
cmd.CommandType = CommandType.Text
cmd.Connection = connection
source.DataSource = cmd.ExecuteReader()
'Assegniamo i dati letti nel bindingsource alle variabili
codice = source.Current!Codice
descrizione = source.Current!Descrizione
unitamisura = source.Current!Um
codiceiva = Convert.ToDouble(source.Current!Iva)
costobase = Convert.ToDouble(source.Current!Costo)
prezzobase = Convert.ToDouble(source.Current!Prezzo)
costoultimo = Convert.ToDouble(source.Current!Costoult)
giacenza = Convert.ToDouble(source.Current!Giacenza)
sconto1 = Convert.ToDouble(source.Current!Sc1)
sconto2 = Convert.ToDouble(source.Current!Sc2)
'Riempiamo le celle con i dati estratti dal BindingSource
With DataGridView1.CurrentRow
.Cells("grdCodice").Value = codice
.Cells("grdDescrizione").Value = descrizione
.Cells("grdUM").Value = source.Current!Um
.Cells("grdQuantita").Value = quantita
If TipoMovimento = "Carico" Then
.Cells("grdPrezzoUnitario").Value = source.Current!CostoUlt
Else
.Cells("grdPrezzoUnitario").Value = source.Current!Prezzo
End If
.Cells("grdSconto1").Value = sconto1
.Cells("grdSconto2").Value = sconto2
.Cells("grdSconto3").Value = 0.0
.Cells("grdSconto4").Value = 0.0
'Per calcolare il prezzo totale di riga dobbiamo tenere presente le
'informazioni che abbiamo gia' in anagrafica, ovvero il costo/prezzo
'e gli sconti
'Blocco di controllo sul tipo movimento
If TipoMovimento = "Carico" Then
If sconto1 > 0 Then
.Cells("grdPrezzoTotale").Value = quantita * (costoultimo * (costoultimo * sconto1 / 100))
Else
.Cells("grdPrezzoTotale").Value = quantita * costoultimo
End If
ElseIf TipoMovimento = "Scarico" Then
If sconto1 > 0 Then
.Cells("grdPrezzoTotale").Value = quantita * (prezzobase * (prezzobase * sconto1 / 100))
Else
.Cells("grdPrezzoTotale").Value = quantita * prezzobase
End If
End If
.Cells("grdAliquotaIva").Value = codiceiva
End With
connection.Close()
End Sub
這就是接收方 DataGridView 中的結果:

now, suppose that I don't want to edit the selected row because I don't need to edit quantity, or price, or whatever, but I just want to insert a new row to keep inserting other products. When I tab until the end of the row I want to create a new row, and that's why I wrote the DataGridView1.Rows.Add() in the RowLeave event of the DataGridView, but that's where the exception occurs:
I'm sorry for all the descriptions are in italian, but if you need any other details feel free to ask.
If there's nothing I can do all that remains is to add a button outside the DataGridView which adds a new row from there. Any other solution is welcomed.
uj5u.com熱心網友回復:
是否有某些原因您不使用DataSource網格?然后,您可以簡單地將行添加到DataSource.
如果您堅持手動添加行,那么您當前遇到的問題是代碼永遠不會向網格“添加”新行。該代碼只是使用網格CurrentRow來設定值。這可能有效,但不會創建網格“新”行。
以這種方式使用網格是有問題的CurrentRow......因此CurrentRow,我建議您在方法中“添加”一個新行,而不是為此使用網格RicercaxCodiceArticolo。像下面這樣的東西應該添加新行,“new-new”空行應該在底部......
Dim newRowIndex As Int32
newRowIndex = DataGridView1.Rows.Add()
With DataGridView1.Rows(newRowIndex)
.Cells("grdCodice").Value = codice
.Cells("grdDescrizione").Value = descrizione
'.....
End With
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/450123.html
