datagrid delete event

M

MattB

I'm trying to wire up a delete even for a datagrid and my even isn't
firing. I think I'm just not understanding something fundamental. Can anyone
tell me why this does not work and/or how to make it work?

Here's the datagrid declaration:

<asp:datagrid id="CartGrid" style="Z-INDEX: 101; LEFT: 48px; POSITION:
absolute; TOP: 176px" runat="server"
OnDeleteCommand="CartGrid_Delete" GridLines="None" BorderWidth="0px"
BackColor="Transparent" BorderStyle="None" Width="624px"
Font-Names="Verdana">
<AlternatingItemStyle BackColor="#FFFFCC"></AlternatingItemStyle>
<HeaderStyle Font-Names="Verdana" Font-Bold="True"
VerticalAlign="Top"></HeaderStyle>
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton"
CommandName="Delete"></asp:ButtonColumn>
<asp:BoundColumn DataField="Qty" HeaderText="Qty"></asp:BoundColumn>
<asp:BoundColumn DataField="descrip"
HeaderText="Item"></asp:BoundColumn>
<asp:BoundColumn DataField="ModDCI"
HeaderText="Modifier"></asp:BoundColumn>
<asp:BoundColumn DataField="guest"
HeaderText="Guest"></asp:BoundColumn>
<asp:BoundColumn DataField="Price"
HeaderText="Price"></asp:BoundColumn>
</Columns>
</asp:datagrid>

--------------------------------

Then in the codebehind, I have this event:
Sub CartGrid_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)

.... 'delete row from datatable

EndSub
 
G

Guest

Hi Matt

Here's a snippet of a simple example that works for me. It has a single bound column, and the delete buttons

<asp:DataGrid id="grdTest" runat="server"><Columns><asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn></Columns></asp:DataGrid

Code behind looks like this..

Private dt As DataTabl

Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Loa

' Build sample dat
dt = New DataTabl
dt.Columns.Add("TestColumn"
Dim s() As String = {"Hi there"
dt.Rows.Add(s

' Bind to gri
grdTest.DataSource = d
grdTest.DataBind(

End Su

Private Sub grdTest_DeleteCommand(ByVal source As Object,
ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs)
Handles grdTest.DeleteComman

End Su

Maybe it's just a difference of style, but I wouldn't set OnDeleteCommand=CartGrid_Delete. Instead, the "handles" clause of the event handler specifies where you want to go when they click on the delete button

hth

Bil

P.S. In a routine like this, you might find it easier to bind the datagrid to the dataset in the PagePrerender, not the PageLoad. That's because the DeleteCommand will execute after page load but before the pageprerender, so if they actually do delete something it will fall through to the page prerender which will correctly display the updated dataset (missing that row). Or, on DeleteCommand, you can refresh the grid, but then you also have to do it on the EditCommand, UpdateCommand, etc

----- MattB wrote: ----

I'm trying to wire up a delete even for a datagrid and my even isn'
firing. I think I'm just not understanding something fundamental. Can anyon
tell me why this does not work and/or how to make it work

Here's the datagrid declaration

<asp:datagrid id="CartGrid" style="Z-INDEX: 101; LEFT: 48px; POSITION
absolute; TOP: 176px" runat="server
OnDeleteCommand="CartGrid_Delete" GridLines="None" BorderWidth="0px
BackColor="Transparent" BorderStyle="None" Width="624px
Font-Names="Verdana"><AlternatingItemStyle BackColor="#FFFFCC"></AlternatingItemStyle><HeaderStyle Font-Names="Verdana" Font-Bold="True
VerticalAlign="Top"></HeaderStyle><Columns><asp:ButtonColumn Text="Delete" ButtonType="PushButton
CommandName="Delete"></asp:ButtonColumn><asp:BoundColumn DataField="Qty" HeaderText="Qty"></asp:BoundColumn><asp:BoundColumn DataField="descrip
HeaderText="Item"></asp:BoundColumn><asp:BoundColumn DataField="ModDCI
HeaderText="Modifier"></asp:BoundColumn><asp:BoundColumn DataField="guest
HeaderText="Guest"></asp:BoundColumn><asp:BoundColumn DataField="Price
HeaderText="Price"></asp:BoundColumn></Columns></asp:datagrid

-------------------------------

Then in the codebehind, I have this event
Sub CartGrid_Delete(ByVal sender As Object, ByVal e A
DataGridCommandEventArgs

.... 'delete row from datatabl

EndSu
 
P

Paul

Matt, if the event does not fire then the handler may have been botched.
Cut the code from handler to clipboard, delete the entire handler
including the declaration, and then reselect it from the dropdowns at
the top of the page and paste your code back into it.

I've seen this several times where system generated event handlers
suddenly stop firing, and I've always fixed it by doing the above.

Don't try to skimp - you have to delete the entire handler not just your
code. Let the system recreate the handler and it will work fine.

~Paul
 
G

Guest

Yah, it could be a case when you copy paste cotrols on the web forms. But you dont have to the copy paste just write Handles (click event handler reference) for the event

for example,


method_name(param_list) Handles btn.Click

hope this will help you,
 
M

MattB

Thanks. I got it working based on your example (but thanks to other
responders too!).

Here's an odd twist: if I change the delete column to a push button instead
of a link button it stops working (just refreshed with the same data -
nothing deleted). Any idea why that is? I can make it a link for now, but I
had originally wanted a push button for the design.

Matt
 
G

Guest

Sorry, no good ideas. I changed the linkbutton to a pushbutton and it fired the event the same way. I think you must have something else going on in there

----- MattB wrote: ----

Thanks. I got it working based on your example (but thanks to othe
responders too!)

Here's an odd twist: if I change the delete column to a push button instea
of a link button it stops working (just refreshed with the same data
nothing deleted). Any idea why that is? I can make it a link for now, but
had originally wanted a push button for the design

Mat

Bill Borg wrote
 
P

Paul

Matt, if the pushbutton doesn't work and linkbutton does you definitely
have something out of whack. You can easily put a break point in the
DataGrid_DeleteCommand() event handler to take a look inside. You might
be surprised to see that it's not even firing.



~Paul
 

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