在c#中添加或删除新记录后刷新gridview

发布于 2024-12-04 13:42:42 字数 1509 浏览 4 评论 0原文

我的页面上有一个网格,我需要刷新 gridview 添加和删除新记录,但它不是?

这是代码:

将行添加到 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();
    }

删除代码:

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

非常感谢...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄想挽回 2024-12-11 13:42:42

您需要将网格重新绑定到数据源:

//delete row from the database

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文