ItemDataBound doesn't fire when I use DeleteCommand

P

Peter Afonin

Hello:

I'm deleteing data using Datagrid, then rebind it. For some reason
ItemDataBound event doesn't fire after DeleteCommand. Why is this? Is it by
design, or I'm missing something? I have some important calculations in
ItemDataBound event. My code for DeleteCommand is below.

I would appreciate your help very much.

Private Sub dgData_DeleteCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
dgData.DeleteCommand
Dim lbl As Label
Dim nID As String
Dim dv As DataView
Try
lbl = CType(e.Item.FindControl("lblID"), Label)
nID = CInt(lbl.Text)
oCnn.ConnectionString = smi_class.Constants.Wip7bConnectionString
oCnn.Open()
With oCmd
..Connection = oCnn
..CommandType = CommandType.StoredProcedure
..CommandText = "dbo.uspDeletePressOrder"
End With
With oCmd.Parameters
..Add("@ID", SqlDbType.Int).Direction = ParameterDirection.Input
..Item("@ID").Value = nID
End With
oCmd.ExecuteNonQuery()

If Not IsNothing(Session("DV")) Then
dv = CType(Session("DV"), DataView)
dv.RowFilter = "ID = " & nID
If dv.Count > 0 Then
dv(0).Delete()
End If
dv.RowFilter = String.Empty
Session("DV") = dv
If dv.Count > 0 Then
Me.dgData.DataSource = dv
Me.dgData.DataBind()
Else
Me.dgData.Visible = False
End If
Else
BindGrid()
End If
dgData.EditItemIndex = -1
CalculateTotals()
Catch ex As Exception
Me.lblError.Text = "Error No.: " & Err.Number.ToString & " - " & ex.ToString
Finally
If Not IsNothing(oCmd) Then
oCmd.Dispose()
End If
If Not IsNothing(cnn) Then
If cnn.State = ConnectionState.Open Then
cnn.Close()
End If
End If
End Try
End Sub

Thank you,
 
B

Bill Priess

By default, when ever one of the DataGrid's events are called
(DeleteCommand, EditCommand, etc), you have to rebind your grid at the end
of the event handler. Try rebinding your data before you leave the
DeleteCommand method.

HTH,
Bill P.
 
P

Peter Afonin

Thank you.

I'm doing it!

If..............................
.................................
If dv.Count > 0 Then
Me.dgData.DataSource = dv
Me.dgData.DataBind()
Else
Me.dgData.Visible = False
End If
Else
BindGrid()
End If

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top