Jurgen Vandw
Jurgen Vandw

Reputation: 651

Gridview usercontrol with delete row functionality doesn't update my gridview after delete

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?

Upvotes: 0

Views: 574

Answers (1)

Suraj Singh
Suraj Singh

Reputation: 4069

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

Upvotes: 1

Related Questions