I’ve got my code to delete rows out of the database when the user clicks on the Delete button in my GridView, but the GridView isn’t updating to reflect the new data changes.
Here’s my code:
<asp:GridView runat="server" ID="grdQuestions" AutoGenerateColumns="false" Width="100%" CellSpacing="10"
PagerSettings-Visible="true">
<HeaderStyle CssClass="aspNetHeader" />
<Columns>
<asp:BoundField DataField="QuestionID" HeaderText="QID" />
<asp:BoundField DataField="ModuleID" HeaderText="Mod #" />
<asp:BoundField DataField="QuestionText" HeaderText="Question" />
<asp:BoundField DataField="CorrectAnswer" HeaderText="Answer" />
<asp:BoundField DataField="AnswerNote" HeaderText="Note" />
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink runat="server" ID="lnkEdit" Text="Edit"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkDelete" Text="Delete" CommandArgument='<%# eval("QuestionID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Protected Sub grdQuestions_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grdQuestions.RowCommand
If e.CommandName = "Delete" Then
sql = "delete from tmp_Questions where QuestionID = " & e.CommandArgument
Dim cmd As New SqlCommand(sql, d.SQLConnection)
d.SQLConnection.Open()
cmd.ExecuteNonQuery()
d.SQLConnection.Close()
GetModuleData()
End If
End Sub
Protected Sub grdQuestions_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles grdQuestions.RowDeleting
GetModuleData()
End Sub
Private Sub GetModuleData()
' ddlModules is a drop down where the user can select a module to filter questions by.
Dim selid As Integer = ddlModules.SelectedItem.Value
sql = "select QuestionID, ModuleID, LEFT(QuestionText, charindex(' ', QuestionText, 30)) as QuestionText, CorrectAnswer," & vbCrLf & _
"AnswerNote from tmp_Questions" & vbCrLf & _
"where ModuleID = " & selid & vbCrLf & _
"order by ModuleID"
Dim cmd As New SqlCommand(sql, d.SQLConnection)
cmd.CommandText = sql
QuestionAdapter.SelectCommand = cmd
QuestionAdapter.Fill(QuestionData)
If QuestionData.Tables(0).Rows.Count > 0 Then
grdQuestions.DataSource = QuestionData
grdQuestions.DataBind()
End If
End Sub
Thanks in advance for having a look!
EDIT 1:
Did some stepping through the code and found that e.CommandArgument is not getting a value in grdQuestions_RowCommand
EDIT 1 (Revised):
The fact that e.CommandArgument wasn’t getting a value was my mistake. I’d changed the way it’s value was being assigned without removing the previous method.
The code runs through grdQuestions_RowCommand and then goes into grdQuestions_RowDeleting and then executes GetModuleData.
I have a feeling that to view changes to the grid, a PostBack might be required, but I’ve removed all DataBinding to an If Not IsPostBack Then block because the alternative caused problems with the DropDownList whose selection is used as a reference to populate the grid (ddlModules selected the Module whose Questions will display in the grid).
You have neither specified the
CommandName="Edit"forlnkEditnorCommandName="Delete"forlnkDelete.I have a feeling that to view changes to the grid, a PostBack might be required
Of course, a postback must occur and the GridView must be databound to it’s DataSource again after you’ve deleted the record. But that postback has been already occured when the user clicked the delete-link and the
grdQuestions_RowDeletinghas been handled.Debug and ensure that the record gets deleted and afterwards the GridView gets databound again.