We help IT Professionals succeed at work.

DataGrid Delete Command

kdeutsch
kdeutsch asked
on
Medium Priority
418 Views
Last Modified: 2012-05-06
I found this code on completing a datagrid delete command but it does not seem to work for me and don't understand why at this point.  The following is mydatagrid and then the code behind.

<asp:datagrid id="myDataGrid" runat="server" BorderWidth="1px" CellPadding="6" BorderColor="#404040" Font-Size="12px" Font-Names="Verdana,Arial,sans-serif" AutoGenerateColumns="False" HeaderStyle-HorizontalAlign="Center"
ItemStyle-HorizontalAlign="Center" au>
<AlternatingItemStyle BackColor="LightSteelBlue"></AlternatingItemStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="White" BackColor="SteelBlue"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="intSurveyID" HeaderText="SurveyID"></asp:BoundColumn>
<asp:BoundColumn DataField="strFullName" HeaderText="Full Name"></asp:BoundColumn>
<asp:BoundColumn DataField="strGrade" HeaderText="Rank/GS-Level"></asp:BoundColumn>
<asp:BoundColumn DataField="strDate" HeaderText="Date of Survey" DataFormatString="{0:yyyy/MM/dd}">
<HeaderStyle Wrap="False"></HeaderStyle>
</asp:BoundColumn>
<asp:ButtonColumn Text="Delete" HeaderText="Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
Private Sub myDataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
        Dim surveyId As String = e.Item.Cells(0).Text
      
        sql = "Delete from tblFTSurvey where intSurveyId = " & surveyId
        insertUpdateDelete(sql)
        '' Binds the grid so that change can be seen 
        myDataGrid.DataSource = myDataTable
        myDataGrid.DataBind()
 
    End Sub

Open in new window

Comment
Watch Question

are you ever reloading or removing the record from  myDataTable after you delete it
Top Expert 2009

Commented:
What burakiewicz is saying is that you need to repopulate the 'myDataTable' after the insertupdateDelete method.

Author

Commented:
Yes,
I actually had the wrong bind in there so I put in the correct one and it still does not delete any information from the database.  When I click it looks like it posts and then posts back with same information.  When i go into SQL the record is still there.

  Private Sub myDataGrid_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
        Dim surveyId As String = e.Item.Cells(0).Text
     
        sql = "Delete from tblFTSurvey where intSurveyId = " & surveyId
       
        insertUpdateDelete(sql)
        '' Binds the grid so that change can be seen
        OrderDataGrid()
    End Sub
what is in this method insertUpdateDelete(sql)? also is surveyId
 showing the correct value

Author

Commented:
Hi,
The method insertupdatedelete is a call to the Db connectiona to take the SQL string and perform that command.  WHen I do a response. write I can't even get into it.  When i click on the link it posts and refreshes without doing the response.write
this might help
ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/fxref_system.web/html/3453f84f-7041-b92e-f859-85fffdc60fbb.htm
add this to your datagrid
OnItemCommand="myDataGrid_Command"
and under columns change your delete to this
<asp:ButtonColumn
                 HeaderText="Delete item"
                 ButtonType="LinkButton"
                 Text="Delete"
                 CommandName="Delete"/>
then add this code

Sub myDataGrid_Command(sender As Object, e As DataGridCommandEventArgs)
 
         Select (CType(e.CommandSource, LinkButton)).CommandName
 
            Case "Delete"
               DeleteItem(e)
 
            ' Add other cases here, if there are multiple ButtonColumns in 
            ' the DataGrid control.
 
            Case Else
               ' Do nothing.
 
         End Select
 
      End Sub
 
      Sub DeleteItem(e As DataGridCommandEventArgs)
 Dim surveyId As String = e.Item.Cells(0).Text
     
        sql = "Delete from tblFTSurvey where intSurveyId = " & surveyId
       
        insertUpdateDelete(sql)
        '' Binds the grid so that change can be seen 
        OrderDataGrid()
 
      End Sub

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Sorry took so long to award points
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.