j'avais recuperé un prograne VB tres pratique pour moi, qui prenet la gestion total d'un Data Grid , supression , modification , ajout
j'aimerai l'adapter au gridview mais j' ai du mal a faire la modification.
Private dsn AS string = ConfigurationSettings.AppSettings("DSN")
' TODO: update the ConnectionString and Command values for your application
Dim ConnectionString As String = dsn
Dim SelectCommand As String = "SELECT si_no, si_adresse, si_cp, si_ville , si_admin from q_site"
Dim isEditing As Boolean = False
dim compteur as integer
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs)
If Not Page.IsPostBack Then
BindGrid()
End If
End Sub
' ---------------------------------------------------------------
'
' DataGrid Commands: Page, Sort, Edit, Update, Cancel, Delete
'
Sub DataGrid_ItemCommand(Sender As Object, E As DataGridCommandEventArgs)
CheckIsEditing(e.CommandName)
End Sub
Sub CheckIsEditing(commandName As String)
If DataGrid1.EditItemIndex <> -1 Then
' we are currently editing a row
If commandName <> "Cancel" And commandName <> "Update" Then
' user's edit changes (If any) will not be committed
Message.Text = "effectuer la mise a jour ou la suppression avant !." & commandname
isEditing = True
End If
End If
End Sub
Sub DataGrid_Edit(Sender As Object, E As DataGridCommandEventArgs)
' turn on editing for the selected row
If Not isEditing Then
DataGrid1.EditItemIndex = e.Item.ItemIndex
BindGrid()
End If
End Sub
Sub DataGrid_Update(Sender As Object, E As DataGridCommandEventArgs)
' keyvalue = l'index du datagrid de la nem ligne
Dim keyValue As String = CStr(DataGrid1.DataKeys(e.Item.ItemIndex))
id = viewstate("counter")
'message.text=id
Dim adr As String = CType(e.Item.Cells(2).Controls(0), TextBox).Text
Dim cp As String = CType(e.Item.Cells(3).Controls(0), TextBox).Text
Dim ville As String = CType(e.Item.Cells(4).Controls(0), TextBox).Text
Dim admin As String = CType(e.Item.Cells(5).Controls(0), TextBox).Text
' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionString)
Dim UpdateCommand As SqlCommand = new SqlCommand()
UpdateCommand.Connection = myConnection
UpdateCommand.Parameters.Add("@si_adresse", SqlDbType.nVarChar, 30).Value = adr
UpdateCommand.Parameters.Add("@si_cp", SqlDbType.nVarChar, 5).Value = cp
UpdateCommand.Parameters.Add("@si_ville", SqlDbType.nVarChar, 10).Value = ville
UpdateCommand.Parameters.Add("@si_admin", SqlDbType.nVarChar, 10).Value = admin
If AddingNew = True Then
UpdateCommand.CommandText = "INSERT INTO q_site(si_adresse,si_cp,si_ville, si_admin) VALUES (@si_adresse, @si_cp, @si_ville, @si_admin)"
Else
UpdateCommand.CommandText = "UPDATE q_site SET si_adresse = @si_adresse, si_cp= @si_cp, si_ville = @si_ville, si_admin= @si_admin WHERE si_no = '" & keyValue & "'"
End If
' execute the command
Try
myConnection.Open()
UpdateCommand.ExecuteNonQuery()
Catch ex as Exception
Message.Text = ex.ToString()
Finally
myConnection.Close()
End Try
' Resort the grid for new records
If AddingNew = True Then
DataGrid1.CurrentPageIndex = 0
AddingNew = false
End If
' rebind the grid
DataGrid1.EditItemIndex = -1
BindGrid()
End Sub
Sub DataGrid_Cancel(Sender As Object, E As DataGridCommandEventArgs)
' cancel editing
DataGrid1.EditItemIndex = -1
BindGrid()
AddingNew = False
End Sub
Sub DataGrid_Delete(Sender As Object, E As DataGridCommandEventArgs)
' delete the selected row
If Not isEditing Then
' the key value for this row is in the DataKeys collection
Dim keyValue As String = CStr(DataGrid1.DataKeys(e.Item.ItemIndex))
' TODO: update the Command value for your application
Dim myConnection As New SqlConnection(ConnectionString)
Dim DeleteCommand As New SqlCommand("DELETE from q_site where si_no='" & keyValue & "'", myConnection)
'execute the command
Try
myConnection.Open()
DeleteCommand.ExecuteNonQuery()
Catch ex as Exception
Message.Text = keyValue
'Message.Text ="Suppression impossible il existe des machines pour cette Societe !"
Finally
myConnection.Close()
End Try
'myConnection.Open()
'DeleteCommand.ExecuteNonQuery()
'myConnection.Close()
' rebind the grid
DataGrid1.CurrentPageIndex = 0
DataGrid1.EditItemIndex = -1
BindGrid()
End If
End Sub
Sub DataGrid_Page(Sender As Object, E As DataGridPageChangedEventArgs)
' display a new page of data
If Not isEditing Then
DataGrid1.EditItemIndex = -1
DataGrid1.CurrentPageIndex = e.NewPageIndex
BindGrid()
End If
End Sub
Sub AddNew_Click(Sender As Object, E As EventArgs)
' add a new row to the end of the data, and set editing mode 'on'
CheckIsEditing("")
If Not isEditing = True Then
' set the flag so we know to do an insert at Update time
AddingNew = True
' add new row to the end of the dataset after binding
' first get the data
Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlDataAdapter(SelectCommand, myConnection)
Dim ds As New DataSet()
myCommand.Fill(ds)
' add a new blank row to the end of the data
Dim rowValues As Object() = { "0", "","","",""}
ds.Tables(0).Rows.Add(rowValues)
' figure out the EditItemIndex, last record on last page
Dim recordCount As Integer = ds.Tables(0).Rows.Count
If recordCount > 0 Then
recordCount -= 1
DataGrid1.CurrentPageIndex = recordCount \ DataGrid1.PageSize
DataGrid1.EditItemIndex = recordCount Mod DataGrid1.PageSize
End If
' databind
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End If
End Sub
' ---------------------------------------------------------------
'
' Helpers Methods:
'
' property to keep track of whether we are adding a new record,
' and save it in viewstate between postbacks
Property AddingNew() As Boolean
Get
Dim o As Object = ViewState("AddingNew")
If o Is Nothing Then
Return False
End If
Return CBool(o)
End Get
Set(ByVal Value As Boolean)
ViewState("AddingNew") = Value
End Set
End Property
Sub BindGrid()
Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlDataAdapter(SelectCommand, myConnection)
dim ds As New DataSet()
myCommand.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind()
End Sub