serim urhan
serim urhan

Reputation: 139

refresh gridview after adding or deleting new record in c#

i've a grid on my page i need to refresh gridview add and delete new record but its not?

here is the code:

Add Row To GridView:

    private void AddClientToGrid()
    {
        int clientID = int.Parse(ddlClient.SelectedValue);
        int clientTypeID = int.Parse(ddlClientType.SelectedValue);
        ClientsAllCDO client = new ClientsBL().ClientsAllSelectByIDAndClientTypeID(clientID, clientTypeID);
        List<ClientsAllCDO> clientList = new List<ClientsAllCDO>();
        clientList = GetClientsFromGrid();
        clientList.Add(client);
        gvClient.DataSource = clientList;
        gvClient.DataBind();
    }

Delete Code:

    protected void btnDeleteClient_Click(object sender, EventArgs e)
    {
        LinkButton btnDeleteClient = sender as LinkButton;
        int rowIndex = int.Parse(btnDeleteClient.Attributes["RowIndex"]);
        if (Request.QueryString["BailiffID"] == null)
        {
            gvClient.DeleteRow(rowIndex);
        }
        else
        {
            int bailiffID = int.Parse(FormCrypto.Decrypt(Request.QueryString["BailiffID"]));
            GridViewRow gvRow = gvClient.Rows[rowIndex];
            int clientTypeID = int.Parse(((Label)gvRow.FindControl("lblClientTypeID")).Text);
            int clientID = int.Parse(((Label)gvRow.FindControl("lblClientID")).Text);
            gvClient.DeleteRow(rowIndex);
            new BailiffClientsBL().BailiffClientDelete(clientID, bailiffID, clientTypeID);
        }         
    }

Thanks alot...

Upvotes: 4

Views: 15815

Answers (1)

James Johnson
James Johnson

Reputation: 46077

You need to rebind the grid to the datasource:

//delete row from the database

GridView1.DataSource = SomeDataRetrievalMethod(); //retrieve the data from the database
GridView1.DataBind();

Upvotes: 4

Related Questions