从gridview问题中删除记录后刷新gridview

时间:2016-02-27 22:05:50

标签: c# asp.net gridview

嗨我有这个问题,我从网格视图中删除了一些记录后,虽然记录已从数据库中删除,但记录仍然存在于网格视图中

只有刷新页面后,记录才会从网格视图中消失

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        gridpurchase.DataSource = s.getpurchase();
        gridpurchase.DataBind();
    }

}

删除代码

protected void gridpurchase_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                string commandName = e.CommandName.ToString().Trim();
                GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

                string PurchaseID = row.Cells[6].Text;
                GridViewRow gvRow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
                Int32 rowind = gvRow.RowIndex;

                switch (commandName)
                {    
                    case "DeletePurchase":
                        if (s.DelPurchase(Convert.ToInt32(PurchaseID)))
                        {
                                string msg = "alert('Purchase Deleted Successfully');";
                               ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');window.location.href='" + Request.RawUrl + "';", true); 
                        }
                        else
                        {
                            string msg = "Purchase Not Deleted";
                            ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + msg + "');window.location.href='" + Request.RawUrl + "';", true);

                        }
                        break;  
                }
            }

            catch (Exception ex)
            {
                Response.Write("Error:" + ex.Message);
            }

        }

aspx代码

                       <asp:GridView ID="gridpurchase" ShowFooter="True" AutoGenerateColumns="False" OnRowCommand="gridpurchase_RowCommand"  runat="server" DataKeyNames="Purchase_ID" OnRowDataBound="OnRowDataBound" CssClass="table table-bordered text-nowrap">

                            <Columns>

                                <asp:TemplateField HeaderText="Edit">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton5" runat="server" CssClass="btn btn-primary btn-xs" CausesValidation="False" CommandName="EditPurchase" Text=""><i class="glyphicon glyphicon-pencil"></i></asp:LinkButton>

                                    </ItemTemplate>
                                    <ControlStyle CssClass="btn btn-primary" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Delete">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton6" runat="server" CausesValidation="False" OnClientClick="return confirm('Are you sure you want to delete this record ?');" CssClass="btn btn-primary btn-xs" CommandName="DeletePurchase" Text="Delete"><i class="glyphicon glyphicon-trash"></i></asp:LinkButton>
                                    </ItemTemplate>
                                    <ControlStyle CssClass="btn btn-danger" />
                                </asp:TemplateField>
                                <asp:TemplateField HeaderText="Print" ShowHeader="False">
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkButton20" runat="server" CausesValidation="False" CommandName="PrintInvoice" CssClass="btn btn-primary btn-xs" Text=""><i class="glyphicon glyphicon-print"></i></asp:LinkButton>
                                    </ItemTemplate>
                                    <ControlStyle CssClass="btn btn-default " />
                                </asp:TemplateField>
                                <asp:BoundField HeaderText="Purchase ID" DataField="Purchase_ID" InsertVisible="False" ItemStyle-CssClass="hiddencol" HeaderStyle-CssClass="hiddencol" ReadOnly="True" SortExpression="Purchase_ID">
                                <asp:BoundField HeaderText="Purchase Date" HeaderText="Purchase Date">

                            </Columns>
                            <EmptyDataTemplate>
                                &quot;No records found&quot;
                            </EmptyDataTemplate>
                            <SelectedRowStyle BackColor="#D1DDF1" ForeColor="#333333" />
                        </asp:GridView> 

1 个答案:

答案 0 :(得分:0)

我认为你必须在删除数据库中的记录后调用DataBind来更新网格。