Imports System.Data
Imports System.Data.SqlClient
Partial Class GridView1
Inherits System.Web.UI.Page
Dim objConn As SqlConnection
Dim objCmd As SqlCommand
Dim strSQL As String
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim strConnString As String
strConnString = "Server=localhost;UID=sa;PASSWORD=1234;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"
objConn = New SqlConnection(strConnString)
objConn.Open()
If Not Page.IsPostBack() Then
BindData()
End If
End Sub
Sub BindData()
strSQL = "SELECT * FROM mydatabase"
Dim dtReader As SqlDataReader
objCmd = New SqlCommand(strSQL, objConn)
dtReader = objCmd.ExecuteReader()
myGridView.DataSource = dtReader
myGridView.DataBind()
dtReader.Close()
dtReader = Nothing
End Sub
Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
objConn.Close()
objConn = Nothing
End Sub
Protected Sub myGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) Handles myGridView.RowCancelingEdit
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
End Sub
Protected Sub myGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles myGridView.RowCommand
If e.CommandName = "Add" Then
Dim txtDate As TextBox = CType(myGridView.FooterRow.FindControl("txtAddDate"), TextBox)
Dim txtClient As TextBox = CType(myGridView.FooterRow.FindControl("txtAddClient"), TextBox)
Dim txtPlace As TextBox = CType(myGridView.FooterRow.FindControl("txtAddPlace"), TextBox)
Dim txtCar As TextBox = CType(myGridView.FooterRow.FindControl("txtAddCar"), TextBox)
Dim txtDriver As TextBox = CType(myGridView.FooterRow.FindControl("txtAddDriver"), TextBox)
Dim txtUsed As TextBox = CType(myGridView.FooterRow.FindControl("txtAddUsed"), TextBox)
strSQL = "INSERT INTO mydatabase (Date,Client,Place,Car,Driver) " & _
" VALUES ('" & txtDate.Text & "','" & txtClient.Text & "','" & txtPlace.Text & "' " & _
" ,'" & txtCar.Text & "','" & txtDriver.Text & "') "
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
BindData()
End If
End Sub
Protected Sub myGridView_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Handles myGridView.RowDeleting
strSQL = "DELETE FROM mydatabase WHERE Date = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
BindData()
End Sub
Protected Sub myGridView_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles myGridView.RowEditing
myGridView.EditIndex = e.NewEditIndex
myGridView.ShowFooter = False
BindData()
End Sub
Protected Sub myGridView_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Handles myGridView.RowUpdating
Dim txtDate As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditDate"), TextBox)
Dim txtClient As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditClient"), TextBox)
Dim txtPlace As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditPlace"), TextBox)
Dim txtCar As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditCar"), TextBox)
Dim txtDriver As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditDriver"), TextBox)
Dim txtUsed As TextBox = CType(myGridView.Rows(e.RowIndex).FindControl("txtEditUsed"), TextBox)
strSQL = "UPDATE mydatabase SET Date = '" & txtDate.Text & "' " & _
" ,Client = '" & txtClient.Text & "' " & _
" ,Place = '" & txtPlace.Text & "' " & _
" ,Car = '" & txtCar.Text & "' " & _
" ,Driver = '" & txtDriver.Text & "' " & _
" WHERE Date = '" & myGridView.DataKeys.Item(e.RowIndex).Value & "'"
objCmd = New SqlCommand(strSQL, objConn)
objCmd.ExecuteNonQuery()
myGridView.EditIndex = -1
myGridView.ShowFooter = True
BindData()
End Sub
End Classฤ