views:

404

answers:

1

I am attempting to prevent the deletion of a Row for my GridView like this:

    void GridView1_RowDeleting(Object sender, GridViewDeleteEventArgs e)
    {

        if (GridView1.Rows[e.RowIndex].Cells[1].ToString() == "root")
        {
            e.Cancel = true;
        }
}

But it's not working, I put a breakpoint in there but it never stops I don't think the event is being captured by this function like MSDN said it would?

Here is the GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataKeyNames="id" DataSourceID="sqlWorkFlowItems" 
        ForeColor="#333333" GridLines="None">
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True"   />
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                ReadOnly="True" SortExpression="id" Visible="False" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="link" HeaderText="link" SortExpression="link" />
            <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" 
                ReadOnly="True" SortExpression="id" Visible="False" />
            <asp:TemplateField HeaderText="parent" SortExpression="parent">
                <EditItemTemplate>
                    <asp:DropDownList ID="cboParent" SelectedValue='<%# Bind("parent") %>' DataSourceID="sqlWorkFlowItems"
                    DataTextField="name" DataValueField="id" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>
                   <asp:DropDownList ID="cboParent" SelectedValue='<%# Bind("parent") %>' DataSourceID="sqlWorkFlowItems"
                    DataTextField="name" DataValueField="id" runat="server" Enabled="false"  />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="company" SortExpression="company">
                <EditItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("company") %>'></asp:Label>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("company") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <EditRowStyle BackColor="#999999" />
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    </asp:GridView>

EDIT:

Also tried adding this to the Page_Load function to no avail:

GridView1.RowDeleting = new GridViewDeleteEventHandler(this.GridView1_RowDeleting);
+1  A: 

You can do this declaratively:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        CellPadding="4" DataKeyNames="id" DataSourceID="sqlWorkFlowItems" 
        ForeColor="#333333" GridLines="None" 
        OnRowDeleting="GridView1_RowDeleting">

Or in code (I think you just missed the +=):

GridView1.RowDeleting += new GridViewDeleteEventHandler(GridView1_RowDeleting);
HectorMac