0

I have a gridview usercontrol with CRUD functionality. I have an OnRowDeleting event and an RowCommand event that checks if my 'Delete' button is clicked.

OnRowDeleting:

protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        grd.EditIndex = -1;
        pnlSelected.Visible = false;
        btnSave.Visible = false;
        btnInsert.Visible = false;
        grd.DataBind();
    }

RowCommand:

protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            LinkButton lnkBtn = (LinkButton)e.CommandSource;    // the button
            GridViewRow myRow = (GridViewRow)lnkBtn.Parent.Parent;  // the row
            GridView myGrid = (GridView)sender; // the gridview

            string item = (string)myGrid.DataKeys[myRow.RowIndex].Value.ToString();
            DataController.DeleteByPrimKey(item);
        }
    }

In my webform I fill up the datasource of my usercontrol. The problem now is when I want to delete a record.

ucShowHouseList.DataSource = myDataSource;

If I delete a record in my gridview, the gridview doesn't show the change. If I refresh the page the record is deleted.

What could be the problem?

EDIT:

I think my problem is that the datasource in the usercontrol doesn't get updated after a delete. So in the Page_Load of my webform I fill up the datasource of my usercontrol:

ucShowHouseList.DataSource = myDataSource;

On row deleting in my usercontrol I do now:

grd.DataSource = DataSource;
grd.DataBind();

DataSource is an IENumerable property in my usercontrol that I fill up on page_load in my webform.

public IEnumerable DataSource;

This returns a collection of items and isn't updated when I delete a row because my page isn't reloaded so the collection can't be updated.

How to solve that?

2
  • 1
    Side-note: instead of lnkBtn.Parent.Parent use lnkBtn.NamingContainer to get the GridViewRow. According to your issue, do you DataBind the GridView after you've changed the DataSource? Commented Nov 22, 2013 at 16:11
  • ok nevermind I solved my problem by refilling my datasource and then rebinding it. My problem is solved now Commented Nov 25, 2013 at 7:42

1 Answer 1

1

You need to call your Gridview Binding function after grd_RowDeleting not grd.DataBind(); Gridview.DataBind need a data source to bind . Here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.