 |
| ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 21st, 2006, 12:06 AM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
deletecommand in the datagrid
hello all,
I have one question on this and i cannot figure it out even i have read many materials on web.
Firstly, my datagrid is dynamically generated by code and i have add a delete button column in it. so i want to delete that row while i click the delete button. i have already add the handler in the code but it still cannot trigger the delete function.
i have trying my hard already and still not get it right. hope anyone can give me some direction. thx
dim dg(table_count) as datagrid
For i = 0 To table_count - 1
dg(i) = New DataGrid
tbldata(i) = New DataTable
dg(i).ID = table_name(i)
dg(i).DataKeyField = "action_id"
strSQL = " Select * from " + table_name(i) + " order by action_id"
'put the data inside the datatable tbldata
tbldata(i) = GetData(strSQL, myConnection)
dg(i).DataSource = tbldata(i)
dg(i).AutoGenerateColumns = False
AddHandler dg(i).DeleteCommand, AddressOf DataGrid_DeleteCommand
' format the columns
FormatColumns(tbldata(i), dg(i), table_name(i), cat)
dg(i).DataBind()
FormatDataGrid(dg(i))
Next
Private Function CreateButtonColumns() As DataGridColumn
Dim bndColumn As New ButtonColumn
bndColumn.HeaderText = "Delete"
bndColumn.CommandName = "Delete"
bndColumn.ButtonType = ButtonColumnType.LinkButton
bndColumn.Text = "delete"
Return bndColumn
End Function
Private Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs )
Dim get_key As String
Dim table_name As String
get_key = e.Item.ItemIndex()
table_name = e.Item.ID
' used to test whether is function is trigger
TextBox1.Text = "aaa"
Try
strSQL = "Delete FROM " + table_name + " WHERE action_id='" + get_key + "';"
Dim myCommand2 As New MySqlCommand(strSQL, myConnection)
myCommand2.ExecuteNonQuery()
'optimize the table after delete the records
Dim strSQL2 = "OPTIMIZE TABLE action_proc"
Dim myCommand3 As New MySqlCommand(strSQL2, myConnection)
myCommand3.ExecuteNonQuery()
myConnection.Close()
Response.Redirect("pc_build_proc_update.aspx")
Catch ex As Exception
myConnection.Close()
End Try
End Sub
|
|

February 22nd, 2006, 03:17 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Post your HTML for the datagrid.
Jim
|
|

February 28th, 2006, 10:19 PM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
umm there are no datagrid code in the HTML
as the datagrid is made by the code
|
|

February 28th, 2006, 11:58 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
Sorry, I missed the part that it was a dynamic datagrid. In your Sub DataGrid_DeleteCommand you don't have a Handles statement. Try:
Private Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid.DeleteCommand
|
|

March 1st, 2006, 10:18 PM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
thanks you for your reply
Private Sub DataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid.DeleteCommand
It do not work because there are no DataGrid there
Also i have add the handler in the code already
AddHandler dg(i).DeleteCommand, AddressOf DataGrid_DeleteCommand
Anyway thanks you
|
|

March 1st, 2006, 11:41 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
|
|
I apologize again, forgetting the dg is dynamic... loosing my mind
I do notice that you have a function CreateButtonColumns(). I see that it creates a button column, I don't see you adding the column to the grid. Can you post that code as well
Jim
|
|

March 2nd, 2006, 02:28 AM
|
|
Registered User
|
|
Join Date: Feb 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Private Sub FormatColumns(ByRef tblData As DataTable, ByRef dg As DataGrid, ByVal type As String, ByVal cat As String)
Dim colDataColumn As DataColumn
For Each colDataColumn In tblData.Columns()
If (colDataColumn.ColumnName.Equals("action_id")) Then
dg.Columns.Add(CreateHyperColumns(colDataColumn, "normal", type, cat))
Else
dg.Columns.Add(CreateBoundColumns(colDataColumn))
End If
Next
dg.Columns.Add(CreateButtonColumns()) this is used for create delete button
dg.Columns.Add(CreateHyperColumns(colDataColumn, "edit", type, cat))
End Sub
|
|
 |