Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
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
  #1 (permalink)  
Old February 21st, 2006, 12:06 AM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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

  #2 (permalink)  
Old February 22nd, 2006, 03:17 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Post your HTML for the datagrid.

Jim

  #3 (permalink)  
Old February 28th, 2006, 10:19 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

umm there are no datagrid code in the HTML

as the datagrid is made by the code

  #4 (permalink)  
Old February 28th, 2006, 11:58 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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

  #5 (permalink)  
Old March 1st, 2006, 10:18 PM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


  #6 (permalink)  
Old March 1st, 2006, 11:41 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

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
  #7 (permalink)  
Old March 2nd, 2006, 02:28 AM
Registered User
 
Join Date: Feb 2006
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom paging in Datagrid with datagrid page count madhusrp ASP.NET 1.0 and 1.1 Professional 12 June 2nd, 2008 01:15 PM
User COntrol Datagrid inside datagrid rodmcleay ASP.NET 1.0 and 1.1 Professional 3 April 14th, 2007 10:11 AM
Creating DataGrid In Repeater/DataGrid liduwan ASP.NET 1.0 and 1.1 Professional 9 March 6th, 2007 03:31 PM
SqlDatasource DeleteCommand gving error Prashant.k.m .NET Framework 2.0 2 May 4th, 2005 11:29 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.