Burak Can
Burak Can

Reputation: 93

The GridView 'GridView1' fired event RowDeleting which wasn't handled , but there is an event

ı have been traying to create dynamic control panel using webusercontrol , delegate,and ADO. Even ı have write the delegate for deleting and editing ı faced with "The GridView 'GridView1' fired event RowDeleting which wasn't handled."problem . Can anybody help me pls here is my codes

    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = this.DataSource;
        GridView1.DataBind();
        GridView1.DataKeyNames = new string[] { this.DataKeyNames };

    }

    public object DataSource { get; set; }
    public string DataKeyNames { get; set; }

    public event GridHander RowDeleting;
    public event GridHander RowSawing;


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        GridViewRow gvr = ((LinkButton)e.CommandSource).Parent.Parent as GridViewRow;
        int rowIndex = gvr.RowIndex;
        object id = GridView1.DataKeys[rowIndex].Value;

        switch (e.CommandName)
        {
            case "Edit":
                GridView1.EditIndex = rowIndex;
                break;

            case "Delete":
                if (RowDeleting != null)
                {
                    GridEventArgs args = new GridEventArgs() 
                    {
                         row=gvr,
                         id=id,
                         rowIndex=rowIndex
                    };
                    RowDeleting.Invoke(e.CommandSource, args);
                }
                break;

            case"Save":
                if (RowSawing != null)
                {
                    GridEventArgs args = new GridEventArgs() 
                    {
                        row = gvr,
                        id = id,
                        rowIndex = rowIndex
                    };
                    RowSawing.Invoke(e.CommandSource, args);

                }
                GridView1.EditIndex = -1;
                break;

            case "Cancel":
                GridView1.EditIndex = -1;
                break;

            default:
                break;
        }
    }
}

//My webform

ublic partial class CategoryControlPanel : System.Web.UI.Page
{
    CategoryResposite _categoryResposite=new CategoryResposite();

    protected void Page_Load(object sender, EventArgs e)
    { 
        ControlPanel.DataSource = _categoryResposite.ListCategories();
        ControlPanel.RowDeleting += ControlPanel_RowDeleting;
        ControlPanel.RowSawing += ControlPanel_RowSawing;
    }

    void ControlPanel_RowSawing(object sender, GridEventArgs e)
    {
        throw new NotImplementedException();
    }

    void ControlPanel_RowDeleting(object sender, GridEventArgs e)
    {
        _categoryResposite.DeleteCategories(Convert.ToInt32(e.id));   
    }

Upvotes: 1

Views: 34087

Answers (4)

Abdullah Ibn Mannan
Abdullah Ibn Mannan

Reputation: 309

You are trying with a command name of Delete for your delete button. So the gridview creates a row deleting event automatically....

You need to change the command argument from Delete to something else like Delete_Product or whatever you want...

Upvotes: 5

queen
queen

Reputation: 1

One of the good things about GridView is that it provides a built-in CommandField Buttons which allows us to perform certain actions like editing, updating,deleting and selecting of GridView data.

To add those command fields mentioned in the GridView you can follow these few steps below: 1. Switch to Design View 2. Right Click on the GridView and Select --> Show Smart Tag --> Add New Columns 3. On the List Select CommandField 4. Check Delete and Edit/Update options then OK

As you can see the Edit and Delete CommandField are automatically added in the last column of GridView. Now we can start to write our codes for editing and updating the information in the GridView.

In-order to perform Edit and Update in GridView we need to use three events ( GridView_RowEditing, GridView_RowCancelingEdit , GridView_RowUpdating). For those who do not know on how to generate Events in GridView you can follow these steps below:

  1. Switch to Design View in Visual Studio Designer
  2. Click on the GridView
  3. Navigate to the GridView Property Pane and then SWITCH to Event Properties
  4. From there you would be able to find the list of events including those three events mentioned above
  5. Double Click on that to generate the Event handler for you

Upvotes: 0

FastGeek
FastGeek

Reputation: 411

The code that you have posted is incomplete (missing the aspx file code), from your description of the problem it sounds as though you have not assigned the RowDeleting event to GridView1.

Inside the opening gridview tag in the aspx file add the assignment as follows:

<asp:gridview ID="..." runat="server" ... OnRowDeleting="<name of event handler>" ...>

If the event handler ControlPanel_RowDeleting is designed to handle a delete from gridview action then insert this as the event handler name. Ensure that you re-bind the gridview after delete so that the changes are shown on postback.

protected void ControlPanel_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    // cancel the automatic delete action
    e.Cancel = true;

    // do the delete
    _categoryResposite.DeleteCategories(Convert.ToInt32(e.id));

    // complete delete action
    GridView1.DataBind();
}

Upvotes: 2

user2250565
user2250565

Reputation:

Try adding protected to the method signatures.

Upvotes: -1

Related Questions