refresh gridview after adding or deleting new record in c#

两盒软妹~` 提交于 2019-12-23 16:49:37

问题


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...


回答1:


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();


来源:https://stackoverflow.com/questions/7409339/refresh-gridview-after-adding-or-deleting-new-record-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!