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.
Poi all'interno di un button richiamo le 2 sub: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
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.codice:CaricamentoDatiFattura() CaricamentoElementiFattura()
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ì:
Infine bisogna selezionare una fattura, in questo modo in un'altra listview ci saranno tutti gli elementi caricati prima nel database.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
Ecco il codice:
Evento selectedindexchange (listview5):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
Grazie ai vostri consigli sono arrivato alla soluzione.codice:Private Sub ListView4_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView4.SelectedIndexChanged ListView5.Items.Clear() CaricamentoElementiFattura() End Sub
Mi scuso ancora.![]()



Rispondi quotando