I have a SQL Sever 2000 Database TableName Names fileds are ID,FIRSTNAME AND LASTNAME.
I bind this Table to a DataGrid named MyDataGrid, added bound columns ID(Read only),First Name,Last Name.
Also added ButtonColumns Edit,Update and Cancel
When I run the form the grid displayed three columns with edit button. If I click edit button the grid displayed ID as read only, first name and last name in text box, the edit button replaced with update cancel button.
if I click either buttons nothing happends. Can anyone help me to fix this: here is my code:
Imports System.Data
Imports System.Data.SqlClient
Sub BindGrid()
Dim conn As SqlConnection
conn = New SqlConnection("server=(local);database=Stardeveloper;User ID=sa;Password=123")
Dim comm As SqlDataAdapter = New SqlDataAdapter("select * from Names", conn)
Dim DS As DataSet = New DataSet()
comm.Fill(DS, "Names")
MyDataGrid.DataSource = DS.Tables("Names").DefaultView
MyDataGrid.DataBind()
End Sub
Public Sub MyDataGrid_EditCommand(ByVal Source As Object, ByVal e As DataGridCommandEventArgs) Handles MyDataGrid.EditCommand
MyDataGrid.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub
Public Sub MyDataGrid_UpdateCommand(ByVal Source As Object, ByVal e As DataGridCommandEventArgs) Handles MyDataGrid.UpdateCommand
Dim Conn As SqlConnection
Dim txtFName As TextBox = e.Item.Cells(1).Controls(0)
Dim txtLName As TextBox = e.Item.Cells(2).Controls(0)
Dim UpdateCmd As String = "UPDATE Names set FIRSTNAME='" & txtFName.Text & "' LASTNAME='" & txtLName.Text & "' WHERE ID='" & e.Item.Cells(0).Text
Conn = New SqlConnection("server=(local);database=Stardeveloper;User ID=sa;Password=123")
Dim Comm As New SqlCommand(UpdateCmd, Conn)
Conn.Open()
Comm.ExecuteNonQuery()
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
Public Sub MyDataGrid_CancelCommand(ByVal Source As Object, ByVal e As DataGridCommandEventArgs) Handles MyDataGrid.CancelCommand
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindGrid()
End Sub
I bind this Table to a DataGrid named MyDataGrid, added bound columns ID(Read only),First Name,Last Name.
Also added ButtonColumns Edit,Update and Cancel
When I run the form the grid displayed three columns with edit button. If I click edit button the grid displayed ID as read only, first name and last name in text box, the edit button replaced with update cancel button.
if I click either buttons nothing happends. Can anyone help me to fix this: here is my code:
Imports System.Data
Imports System.Data.SqlClient
Sub BindGrid()
Dim conn As SqlConnection
conn = New SqlConnection("server=(local);database=Stardeveloper;User ID=sa;Password=123")
Dim comm As SqlDataAdapter = New SqlDataAdapter("select * from Names", conn)
Dim DS As DataSet = New DataSet()
comm.Fill(DS, "Names")
MyDataGrid.DataSource = DS.Tables("Names").DefaultView
MyDataGrid.DataBind()
End Sub
Public Sub MyDataGrid_EditCommand(ByVal Source As Object, ByVal e As DataGridCommandEventArgs) Handles MyDataGrid.EditCommand
MyDataGrid.EditItemIndex = e.Item.ItemIndex
BindGrid()
End Sub
Public Sub MyDataGrid_UpdateCommand(ByVal Source As Object, ByVal e As DataGridCommandEventArgs) Handles MyDataGrid.UpdateCommand
Dim Conn As SqlConnection
Dim txtFName As TextBox = e.Item.Cells(1).Controls(0)
Dim txtLName As TextBox = e.Item.Cells(2).Controls(0)
Dim UpdateCmd As String = "UPDATE Names set FIRSTNAME='" & txtFName.Text & "' LASTNAME='" & txtLName.Text & "' WHERE ID='" & e.Item.Cells(0).Text
Conn = New SqlConnection("server=(local);database=Stardeveloper;User ID=sa;Password=123")
Dim Comm As New SqlCommand(UpdateCmd, Conn)
Conn.Open()
Comm.ExecuteNonQuery()
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
Public Sub MyDataGrid_CancelCommand(ByVal Source As Object, ByVal e As DataGridCommandEventArgs) Handles MyDataGrid.CancelCommand
MyDataGrid.EditItemIndex = -1
BindGrid()
End Sub
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
BindGrid()
End Sub