18/05/10 5:33
Federico_VbNet_Programmer
Allora io farei così:
-All'evento indexchange della listbox ci metto di caricare i dati da Listbox1.SelectdItem (sarebbe l'elemento selezionato)
-Il primo dato lo mette nella textbox1,il secondo nella textbox2,e cos' via...
Dimmi se hai risolto in questo modo
-All'evento indexchange della listbox ci metto di caricare i dati da Listbox1.SelectdItem (sarebbe l'elemento selezionato)
-Il primo dato lo mette nella textbox1,il secondo nella textbox2,e cos' via...
Dimmi se hai risolto in questo modo
aaa
18/05/10 12:20
Il Totem
Ti suggerisco il controllo BindingNavigator:
totem.altervista.org/guida/versione3/…
E al posto di una listbox, una datagridview:
totem.altervista.org/guida/versione3/…
L'esempio di quest'ultimo link è proprio uguale a quello che devi fare tu.
Il gestore di database usato è MySql, ma fa poca differenza se passi a dataset tipizzati.
totem.altervista.org/guida/versione3/…
E al posto di una listbox, una datagridview:
totem.altervista.org/guida/versione3/…
L'esempio di quest'ultimo link è proprio uguale a quello che devi fare tu.
Il gestore di database usato è MySql, ma fa poca differenza se passi a dataset tipizzati.
Ultima modifica effettuata da Il Totem 18/05/10 12:22
aaa
18/05/10 13:27
Grubby
Postato originariamente da Il Totem:
Ti suggerisco il controllo BindingNavigator:
totem.altervista.org/guida/versione3/…
Ti suggerisco il controllo BindingNavigator:
totem.altervista.org/guida/versione3/…
Non funziona Non trova la pagina
Comunque ho risolto come mi ha suggerito Totem
Ora ho un'altro probblema...
Sto utilizzando un datagrindview con sotto 2 bottoni con scritto cancella e modifica record.
Come ultima istruzione nel bottone modifica e cancella dovrei fare una refresh del datagridview ma ogni volta che ricarico i record, gli aggiunge al datagrid, mentre dovrebbe sostituirli.
Sto cercando un comando per poter pulire tutte le mie righe.
è molto che cerco ma google non mi aiuta
Comunque complimenti Totem per la bella guida
Hai fatto tutto da solo?
aaa
18/05/10 17:13
Il Totem
DataGridView.Rows.Clear()
E sì, ho fatto tutto da solo (ora il link è a posto).
E sì, ho fatto tutto da solo (ora il link è a posto).
Ultima modifica effettuata da Il Totem 19/05/10 18:38
aaa
18/05/10 17:36
Grubby
Davvero complimenti...
Grazie per il link
Comunque datagridview.rows.clear() non funziona
Quando faccio la refresh del datagrid mi copia gli stessi dati 3 volte....
o meglio la prima copia dei dati è la copia prima della modifica del database,
la seconda copia è uguale alla prima (non so da dove venga fuori),
la terza sono i dai aggiornati del database.
Comunque io non passo da dataset (anche perchè non so usarli), pongo direttamente da database dentro delle variabili che poi sistemo all'interno del datagrid.
E' sbagliato?
Vi posto il codice anche se è un po' lungo
Public Class Amministratore
Dim miodataset As New DataSet
Dim miodataadapter As New OleDb.OleDbDataAdapter
Dim oledbconnection1 = New OleDb.OleDbConnection
Dim item_nick, item_psw, item_nome, item_cognome, item_e_mail, item_stato_reg, item_rango As String
Private Sub Amministratore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
caricamento()
End Sub
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
indice = DataGridView1.CurrentCellAddress.Y
modifica()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oledbconnection1 = New OleDb.OleDbConnection
miodataadapter.SelectCommand = New OleDb.OleDbCommand
miodataadapter.SelectCommand.Connection = oledbconnection1
If TxtCognome.Text = "" Or TxtNome.Text = "" Or TxtNick.Text = "" Or TxtPassword.Text = "" Or TxtCognome.Text = "" Then
MsgBox("Impossibile modificare i dati dell'utente. Controllare di aver compilato tutti i campi correttamente."
Else
Dim x1nick, x1password, x1cognome, x1nome, x1stattivazione, x1rango As String
x1nome = TxtNome.Text
x1cognome = TxtCognome.Text
x1nick = TxtNick.Text
x1password = TxtPassword.Text
x1rango = TxtRango.Text
x1stattivazione = Txtstatoreg.Text
miodataadapter.UpdateCommand = New OleDb.OleDbCommand
miodataadapter.UpdateCommand.Connection = oledbconnection1
oledbconnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=""C:\Users\Lucas\Desktop\dbREGISTRAZIONE.mdb"";Mod" & _
"e=ReadWrite;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB" & _
":System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prope" & _
"rties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database" & _
"=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Copy Locale on Com" & _
"pact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
oledbconnection1.Open()
miodataadapter.UpdateCommand.CommandText = "update TABREGISTRAZIONE set NICK = '" & x1nick & "', PSW = '" & x1password & "', NOME = '" & x1nome & "', COGNOME = '" & x1cognome & "', STATTIVAZIONE = '" & x1stattivazione & "', RANGO = '" & x1rango & "' where EMAIL = '" & e_mail & "'"
miodataadapter.UpdateCommand.ExecuteNonQuery()
oledbconnection1.Close()
MsgBox("I dati dell'utente sono stati modificati correttamente", MsgBoxStyle.MsgBoxSetForeground, "Avviso"
DataGridView1.Rows.Clear()
caricamento()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TxtCognome.Text = "" Or TxtNome.Text = "" Or TxtNick.Text = "" Or TxtPassword.Text = "" Or TxtCognome.Text = "" Then
MsgBox("Nessun utente selezionato"
Else
If MessageBox.Show("Vuoi davvero cancellare?", "Cancella", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
miodataadapter.DeleteCommand = New OleDb.OleDbCommand
miodataadapter.DeleteCommand.Connection = oledbconnection1
oledbconnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=""C:\Users\Lucas\Desktop\dbREGISTRAZIONE.mdb"";Mod" & _
"e=ReadWrite;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB" & _
":System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prope" & _
"rties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database" & _
"=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Copy Locale on Com" & _
"pact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
oledbconnection1.Open()
miodataadapter.DeleteCommand.CommandText = "DELETE FROM TABREGISTRAZIONE where EMAIL = '" & e_mail & "'"
miodataadapter.DeleteCommand.ExecuteNonQuery()
oledbconnection1.Close()
MsgBox("l'utente è stato cancellato", MsgBoxStyle.MsgBoxSetForeground, "Avviso"
End If
caricamento()
End If
End Sub
Private Sub modifica()
Dim item_nick, item_psw, item_nome, item_cognome, item_stato_reg, item_rango As String
miodataadapter.SelectCommand = New OleDb.OleDbCommand
miodataadapter.SelectCommand.Connection = oledbconnection1
oledbconnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=""C:\Users\Lucas\Desktop\dbREGISTRAZIONE.mdb"";Mod" & _
"e=ReadWrite;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB" & _
":System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prope" & _
"rties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database" & _
"=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Copy Locale on Com" & _
"pact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
oledbconnection1.Open()
miodataadapter.SelectCommand.CommandText = "SELECT * FROM TABREGISTRAZIONE"
miodataadapter.Fill(miodataset, "ElencoStudenti"
oledbconnection1.Close()
For i = 0 To miodataset.Tables("ElencoStudenti".Rows().Count - 1
If i = indice Then
item_nick = miodataset.Tables("ElencoStudenti".Rows(i).Item("NICK"
item_psw = miodataset.Tables("ElencoStudenti".Rows(i).Item("PSW"
item_nome = miodataset.Tables("ElencoStudenti".Rows(i).Item("NOME"
item_cognome = miodataset.Tables("ElencoStudenti".Rows(i).Item("COGNOME"
item_stato_reg = miodataset.Tables("ElencoStudenti".Rows(i).Item("STATTIVAZIONE"
item_rango = miodataset.Tables("ElencoStudenti".Rows(i).Item("RANGO"
e_mail = miodataset.Tables("ElencoStudenti".Rows(i).Item("EMAIL"
TxtNome.Text = item_nome
TxtCognome.Text = item_cognome
TxtNick.Text = item_nick
TxtPassword.Text = item_psw
Txtstatoreg.Text = item_stato_reg
TxtRango.Text = item_rango
End If
Next
End Sub
Private Sub caricamento()
miodataadapter.SelectCommand = New OleDb.OleDbCommand
miodataadapter.SelectCommand.Connection = oledbconnection1
oledbconnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=""C:\Users\Lucas\Desktop\dbREGISTRAZIONE.mdb"";Mod" & _
"e=ReadWrite;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB" & _
":System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prope" & _
"rties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database" & _
"=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Copy Locale on Com" & _
"pact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
oledbconnection1.Open()
miodataadapter.SelectCommand.CommandText = "SELECT * FROM TABREGISTRAZIONE"
miodataadapter.Fill(miodataset, "ElencoStudenti"
oledbconnection1.Close()
For i = 0 To miodataset.Tables("ElencoStudenti".Rows().Count - 1
item_nick = miodataset.Tables("ElencoStudenti".Rows(i).Item("NICK"
item_psw = miodataset.Tables("ElencoStudenti".Rows(i).Item("PSW"
item_nome = miodataset.Tables("ElencoStudenti".Rows(i).Item("NOME"
item_cognome = miodataset.Tables("ElencoStudenti".Rows(i).Item("COGNOME"
item_e_mail = miodataset.Tables("ElencoStudenti".Rows(i).Item("EMAIL"
item_stato_reg = miodataset.Tables("ElencoStudenti".Rows(i).Item("STATTIVAZIONE"
item_rango = miodataset.Tables("ElencoStudenti".Rows(i).Item("RANGO"
DataGridView1.Rows.Add()
DataGridView1.Item(0, i).Value = item_nick
DataGridView1.Item(1, i).Value = item_psw
DataGridView1.Item(2, i).Value = item_nome
DataGridView1.Item(3, i).Value = item_cognome
DataGridView1.Item(4, i).Value = item_e_mail
DataGridView1.Item(5, i).Value = item_stato_reg
DataGridView1.Item(6, i).Value = item_rango
Next
End Sub
End Class
Se non capite qualcosa chiedete anche perchè non ho commentato
Grazie in anticipo
Grazie per il link
Comunque datagridview.rows.clear() non funziona
Quando faccio la refresh del datagrid mi copia gli stessi dati 3 volte....
o meglio la prima copia dei dati è la copia prima della modifica del database,
la seconda copia è uguale alla prima (non so da dove venga fuori),
la terza sono i dai aggiornati del database.
Comunque io non passo da dataset (anche perchè non so usarli), pongo direttamente da database dentro delle variabili che poi sistemo all'interno del datagrid.
E' sbagliato?
Vi posto il codice anche se è un po' lungo
Public Class Amministratore
Dim miodataset As New DataSet
Dim miodataadapter As New OleDb.OleDbDataAdapter
Dim oledbconnection1 = New OleDb.OleDbConnection
Dim item_nick, item_psw, item_nome, item_cognome, item_e_mail, item_stato_reg, item_rango As String
Private Sub Amministratore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
caricamento()
End Sub
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
indice = DataGridView1.CurrentCellAddress.Y
modifica()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oledbconnection1 = New OleDb.OleDbConnection
miodataadapter.SelectCommand = New OleDb.OleDbCommand
miodataadapter.SelectCommand.Connection = oledbconnection1
If TxtCognome.Text = "" Or TxtNome.Text = "" Or TxtNick.Text = "" Or TxtPassword.Text = "" Or TxtCognome.Text = "" Then
MsgBox("Impossibile modificare i dati dell'utente. Controllare di aver compilato tutti i campi correttamente."
Else
Dim x1nick, x1password, x1cognome, x1nome, x1stattivazione, x1rango As String
x1nome = TxtNome.Text
x1cognome = TxtCognome.Text
x1nick = TxtNick.Text
x1password = TxtPassword.Text
x1rango = TxtRango.Text
x1stattivazione = Txtstatoreg.Text
miodataadapter.UpdateCommand = New OleDb.OleDbCommand
miodataadapter.UpdateCommand.Connection = oledbconnection1
oledbconnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=""C:\Users\Lucas\Desktop\dbREGISTRAZIONE.mdb"";Mod" & _
"e=ReadWrite;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB" & _
":System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prope" & _
"rties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database" & _
"=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Copy Locale on Com" & _
"pact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
oledbconnection1.Open()
miodataadapter.UpdateCommand.CommandText = "update TABREGISTRAZIONE set NICK = '" & x1nick & "', PSW = '" & x1password & "', NOME = '" & x1nome & "', COGNOME = '" & x1cognome & "', STATTIVAZIONE = '" & x1stattivazione & "', RANGO = '" & x1rango & "' where EMAIL = '" & e_mail & "'"
miodataadapter.UpdateCommand.ExecuteNonQuery()
oledbconnection1.Close()
MsgBox("I dati dell'utente sono stati modificati correttamente", MsgBoxStyle.MsgBoxSetForeground, "Avviso"
DataGridView1.Rows.Clear()
caricamento()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If TxtCognome.Text = "" Or TxtNome.Text = "" Or TxtNick.Text = "" Or TxtPassword.Text = "" Or TxtCognome.Text = "" Then
MsgBox("Nessun utente selezionato"
Else
If MessageBox.Show("Vuoi davvero cancellare?", "Cancella", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
miodataadapter.DeleteCommand = New OleDb.OleDbCommand
miodataadapter.DeleteCommand.Connection = oledbconnection1
oledbconnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=""C:\Users\Lucas\Desktop\dbREGISTRAZIONE.mdb"";Mod" & _
"e=ReadWrite;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB" & _
":System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prope" & _
"rties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database" & _
"=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Copy Locale on Com" & _
"pact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
oledbconnection1.Open()
miodataadapter.DeleteCommand.CommandText = "DELETE FROM TABREGISTRAZIONE where EMAIL = '" & e_mail & "'"
miodataadapter.DeleteCommand.ExecuteNonQuery()
oledbconnection1.Close()
MsgBox("l'utente è stato cancellato", MsgBoxStyle.MsgBoxSetForeground, "Avviso"
End If
caricamento()
End If
End Sub
Private Sub modifica()
Dim item_nick, item_psw, item_nome, item_cognome, item_stato_reg, item_rango As String
miodataadapter.SelectCommand = New OleDb.OleDbCommand
miodataadapter.SelectCommand.Connection = oledbconnection1
oledbconnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=""C:\Users\Lucas\Desktop\dbREGISTRAZIONE.mdb"";Mod" & _
"e=ReadWrite;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB" & _
":System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prope" & _
"rties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database" & _
"=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Copy Locale on Com" & _
"pact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
oledbconnection1.Open()
miodataadapter.SelectCommand.CommandText = "SELECT * FROM TABREGISTRAZIONE"
miodataadapter.Fill(miodataset, "ElencoStudenti"
oledbconnection1.Close()
For i = 0 To miodataset.Tables("ElencoStudenti".Rows().Count - 1
If i = indice Then
item_nick = miodataset.Tables("ElencoStudenti".Rows(i).Item("NICK"
item_psw = miodataset.Tables("ElencoStudenti".Rows(i).Item("PSW"
item_nome = miodataset.Tables("ElencoStudenti".Rows(i).Item("NOME"
item_cognome = miodataset.Tables("ElencoStudenti".Rows(i).Item("COGNOME"
item_stato_reg = miodataset.Tables("ElencoStudenti".Rows(i).Item("STATTIVAZIONE"
item_rango = miodataset.Tables("ElencoStudenti".Rows(i).Item("RANGO"
e_mail = miodataset.Tables("ElencoStudenti".Rows(i).Item("EMAIL"
TxtNome.Text = item_nome
TxtCognome.Text = item_cognome
TxtNick.Text = item_nick
TxtPassword.Text = item_psw
Txtstatoreg.Text = item_stato_reg
TxtRango.Text = item_rango
End If
Next
End Sub
Private Sub caricamento()
miodataadapter.SelectCommand = New OleDb.OleDbCommand
miodataadapter.SelectCommand.Connection = oledbconnection1
oledbconnection1.ConnectionString = "Jet OLEDB:Global Partial Bulk Ops=2;Jet OLEDB:Registry Path=;Jet OLEDBatabase L" & _
"ocking Mode=1;Data Source=""C:\Users\Lucas\Desktop\dbREGISTRAZIONE.mdb"";Mod" & _
"e=ReadWrite;Jet OLEDB:Engine Type=5;Provider=""Microsoft.Jet.OLEDB.4.0"";Jet OLEDB" & _
":System database=;Jet OLEDB:SFP=False;persist security info=False;Extended Prope" & _
"rties=;Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:Encrypt Database" & _
"=False;Jet OLEDB:Create System Database=False;Jet OLEDBon't Copy Locale on Com" & _
"pact=False;User ID=Admin;Jet OLEDB:Global Bulk Transactions=1"
oledbconnection1.Open()
miodataadapter.SelectCommand.CommandText = "SELECT * FROM TABREGISTRAZIONE"
miodataadapter.Fill(miodataset, "ElencoStudenti"
oledbconnection1.Close()
For i = 0 To miodataset.Tables("ElencoStudenti".Rows().Count - 1
item_nick = miodataset.Tables("ElencoStudenti".Rows(i).Item("NICK"
item_psw = miodataset.Tables("ElencoStudenti".Rows(i).Item("PSW"
item_nome = miodataset.Tables("ElencoStudenti".Rows(i).Item("NOME"
item_cognome = miodataset.Tables("ElencoStudenti".Rows(i).Item("COGNOME"
item_e_mail = miodataset.Tables("ElencoStudenti".Rows(i).Item("EMAIL"
item_stato_reg = miodataset.Tables("ElencoStudenti".Rows(i).Item("STATTIVAZIONE"
item_rango = miodataset.Tables("ElencoStudenti".Rows(i).Item("RANGO"
DataGridView1.Rows.Add()
DataGridView1.Item(0, i).Value = item_nick
DataGridView1.Item(1, i).Value = item_psw
DataGridView1.Item(2, i).Value = item_nome
DataGridView1.Item(3, i).Value = item_cognome
DataGridView1.Item(4, i).Value = item_e_mail
DataGridView1.Item(5, i).Value = item_stato_reg
DataGridView1.Item(6, i).Value = item_rango
Next
End Sub
End Class
Se non capite qualcosa chiedete anche perchè non ho commentato
Grazie in anticipo
aaa
19/05/10 18:45
Il Totem
Puoi usare anche questo modo, solo che viene molto confusionario...
Rows.Clear() potrebbe non funzionare nel caso la datagridview abbia un binding verso un dataset, ma poiché aggiungi i dati manualmente deve funzionare...
Rows.Clear() potrebbe non funzionare nel caso la datagridview abbia un binding verso un dataset, ma poiché aggiungi i dati manualmente deve funzionare...
aaa