Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.NET DataGrid EditCommand Events Not fired

Status
Not open for further replies.

abu22b

Programmer
Mar 19, 2001
8
0
0
SA
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
 
If you are simply trying to make an editable web grid I suggest you follow this walkthrough:


For a more extensive look at the control read:


--------------------------------------------------------------------------------------------------------------------------------------------

Need help finding an answer?

Try the search facility ( or read FAQ222-2244 on how to get better results.
 
You need the EditItemTemplate along with your ItemTemplate:
<ItemTemplate>
<%# Container.DataItem("stor_id") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="stor_id" Columns="5"
Text='<%# Container.DataItem("stor_id") %>' Runat="server" />
</EditItemTemplate>
 
Please check InitializeComponent(). It should have reference to your MyDataGrid_EditCommand function like this.

this.MyDataGrid.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.MyDataGrid_EditCommand);
Hope this helps:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top