Buongiorno,
premetto che provengo da programmazione vb.net, stavo cercando di convertire alcuni progetti desktop in ASP.NET, navigando sul web ho trovato moltissimo materiale per la greazione di page web e i suoi controlli.
Quello che non ho trovato è l'insert, update e cancel dei dati caricati all'interno della GridView con AutoGenerateColumns="true".
Io in VB.net ho gestito una datagridview con il codice seguente
Popolo la datagridview:
Public Sub Carica_Dati(ByVal Database As String, ByVal Tabella As String)
Try
'Dim Database As String = Principale.ListView2.Items(Principale.ListView2.FocusedItem.Index).SubItems(0).Text
'Dim Tabella As String = ListView1.Items(ListView1.FocusedItem.Index).SubItems(0).Text
frmPrincipale.DataGridView1.Columns.Clear()
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = ""
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
frmPrincipale.DataGridView1.Columns.Insert(0, checkBoxColumn)
'strSQL = "SELECT * FROM " & Id.Text & ""
'strSQL = "SELECT * FROM '" & Database & "'.'" & Tabella & "'"
strSQL = "SELECT * FROM " & Database & "." & Tabella & ""
Dim data_Adapter As MySqlDataAdapter
' Create the SqlDataAdapter.
data_Adapter = New MySqlDataAdapter(strSQL, Connection)
' Map Table to Contacts.
data_Adapter.TableMappings.Add("Table", Tabella)
' Fill the DataSet.
m_DataSet = New DataSet()
data_Adapter.Fill(m_DataSet)
data_Adapter.FillSchema(m_DataSet, SchemaType.Source)
' Bind the DataGrid control to the Contacts DataTable.
'DataGrid1.SetDataBinding(m_DataSet, "authors")
frmPrincipale.DataGridView1.DataSource = m_DataSet
frmPrincipale.DataGridView1.DataMember = Tabella
Catch ex As MySqlException
End Try
End Sub
Aggiorno i dati sia in Insert che in Update:
Public Sub Commit(ByVal Database As String, ByVal Tabella As String)
If m_DataSet.HasChanges() Then
Dim data_adapter As MySqlDataAdapter
Dim Command_Builder As MySqlCommandBuilder
Try
strSQL = "SELECT * FROM " & Database & "." & Tabella & ""
' Create the DataAdapter.
data_adapter = New MySqlDataAdapter(strSQL, Connection)
' Map Table to Contacts.
data_adapter.TableMappings.Add("Table", Tabella)
' Make the CommandBuilder generate the
' insert, update, and delete commands.
Command_Builder = New MySqlCommandBuilder(data_adapter)
' Save the changes.
data_adapter.Update(m_DataSet)
Catch ex As Exception
MsgBox("Commit" & " " & ex.Message.ToString)
End Try
End If
End Sub
Ho creatola mia Gridview:
<asp:GridView ID="gvTable" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
runat="server" AutoGenerateColumns="true" AllowPaging="true" OnPageIndexChanging="gvTable_PageIndexChanging" PageSize="5">
</asp:GridView>
Popolo la Gridview:
Private Sub PopolaTabella(ByVal Database As String, ByVal Tabella As String)
Try
strSQL = "SELECT * FROM " & Database & "." & Tabella & ""
Dim data_Adapter As MySqlDataAdapter
' Create the SqlDataAdapter.
data_Adapter = New MySqlDataAdapter(strSQL, Connection)
' Map Table to Contacts.
data_Adapter.TableMappings.Add("Table", Tabella)
' Fill the DataSet.
m_DataSet = New DataSet()
data_Adapter.Fill(m_DataSet)
data_Adapter.FillSchema(m_DataSet, SchemaType.Source)
' Bind the DataGrid control to the Contacts DataTable.
'DataGrid1.SetDataBinding(m_DataSet, "authors")
gvTable.DataSource = m_DataSet
gvTable.DataBind()
Catch ex As MySqlException
End Try
End Sub
Aquesto punto però l'aggiornamento dei dati non riesco a farlo, soprattutto non so come creare la struttura del gridview con campi editabili visto che le rows vengono create automaticamente.
Ringrazio anticipatamente