Si lo so, comunque ho risolto così:
Tabelle
fatture:

dettaglio:

Per caricare i dati, faccio così:
Dichiaro 2 sub, una per i dati generali e un'altra per gli elementi della fattura.
codice:
Public Sub CaricamentoDatiFattura() Dim strSQL As String = "INSERT INTO fatture (Numero,Codice,Cliente,Data) Values ('" & _
txtNumero.Text & "', '" & _
txtCliente.Text & "', '" & _
ComboCliente.Text & "', '" & _
txtData.Text & "')"
CONNECTION.Open()
cmd = New MySqlCommand(strSQL, CONNECTION)
dr = cmd.ExecuteReader()
dr.Close()
cmd.Dispose()
CONNECTION.Close()
End Sub
Public Sub CaricamentoElementiFattura()
For Each x As ListViewItem In ListView1.Items
Dim strSQL As String = "INSERT INTO dettaglio (Numero,Descrizione,Prezzo,Imposta,Inps,RitenutaAcconto,Note) Values ('" & _
txtNumero.Text & "', '" & _
x.SubItems(0).Text & "', '" & _
x.SubItems(1).Text & "','" & _
x.SubItems(2).Text & "','" & _
x.SubItems(3).Text & "','" & _
x.SubItems(4).Text & "','" & _
txtNote.Text & "')"
CONNECTION.Open()
cmd = New MySqlCommand(strSQL, CONNECTION)
dr = cmd.ExecuteReader()
dr.Close()
cmd.Dispose()
CONNECTION.Close()
Next
End Sub
Poi all'interno di un button richiamo le 2 sub:
codice:
CaricamentoDatiFattura()
CaricamentoElementiFattura()
In questo modo nella tabella fatture vengono inserite le informazioni generali, mentre nella tabella dettaglio le informazioni degli elementi, come id univoco ho utilizzato il numero della fattura.
Chiave Primaria tabella fatture: Numero(il numero della fattura)
Chiave Primaria tabella dettaglio : id(campo int, con auto increment)
Il collegamento l'ho fatto tra la chiave primaria numero della tabella fatture e il campo numero della tabella dettaglio
Per caricare le fatture nella listview ho fatto così:
codice:
Public Sub CaricamentoFattureClienti() strSQL = "SELECT * FROM fatture"
CONNECTION.Open()
cmd = New MySqlCommand(strSQL, CONNECTION)
dr = cmd.ExecuteReader()
Do While dr.Read()
NumeroFattura = dr("Numero").ToString
DataFattura = dr("Data").ToString
ClienteFattura = dr("Cliente").ToString
CodiceFattura = dr("Codice").ToString
Dim lv As ListViewItem = ListView4.Items.Add(NumeroFattura)
lv.SubItems.Add(DataFattura)
lv.SubItems.Add(ClienteFattura)
lv.SubItems.Add(CodiceFattura)
Loop
dr.Close()
cmd.Dispose()
CONNECTION.Close()
End Sub
Infine bisogna selezionare una fattura, in questo modo in un'altra listview ci saranno tutti gli elementi caricati prima nel database.
Ecco il codice:
codice:
Public Sub CaricamentoElementiFattura()
strSQL = "SELECT * FROM dettaglio where Numero ='" & ListView4.SelectedItems(0).Text & "'"
CONNECTION.Open()
cmd = New MySqlCommand(strSQL, CONNECTION)
dr = cmd.ExecuteReader()
Do While dr.Read()
NumeroFattura = dr("Numero").ToString
DescrizioneFattura = dr("Descrizione").ToString
PrezzoFattura = dr("Prezzo").ToString
IvaFattura = dr("Imposta").ToString
InpsFattura = dr("Inps").ToString
RitenutaAccontoFattura = dr("RitenutaAcconto").ToString
Dim lv As ListViewItem = ListView5.Items.Add(NumeroFattura)
lv.SubItems.Add(DescrizioneFattura)
lv.SubItems.Add(PrezzoFattura)
lv.SubItems.Add(IvaFattura)
lv.SubItems.Add(RitenutaAccontoFattura)
Loop
dr.Close()
cmd.Dispose()
CONNECTION.Close()
End Sub
Evento selectedindexchange (listview5):
codice:
Private Sub ListView4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView4.SelectedIndexChanged ListView5.Items.Clear()
CaricamentoElementiFattura()
End Sub
Grazie ai vostri consigli sono arrivato alla soluzione.
Mi scuso ancora.