GridView does not refresh after event deletion

I am having a strange problem with asp.net GridView . I have a GridView related to LinqDataSource . In the GridView row delete event, I delete the row from the database, but the grid is not updated (even after BINDING to the data source).

When I set a breakpoint, I could see that the OnRowDeleting event is OnRowDeleting after the LinqDS_Selecting event. But he was not fired after the deletion event! Maybe this is the reason? What am I doing wrong?

Can someone please help. Thanks a lot at Advance.

.aspx file:

 <asp:LinqDataSource ID="LinqDS" runat="server" OnSelecting="LinqDS_Selecting"> </asp:LinqDataSource> <asp:GridView DataSourceID = "LinqDS" ID = "gv1" runat = "server" DataKeyNames = "InstructionId" EnableViewState = "false" OnRowDataBound = "gv1_RowDataBound" OnRowDeleting = "gv1_RowDeleting" OnRowCommand = "gv1_RowCommand" PageSize = "30" > <Columns> <!-- My colums ---> </Columns> </asp:GridView> 

.aspx.cs

 protected void Page_Load(object sender, EventArgs e) { } protected void LinqDS_Selecting(object sender, LinqDataSourceSelectEventArgs e) { // my linq to sql query var query = from .... .... ; e.Result = query; } protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e) { int instructionId = (int)gv1.DataKeys[e.RowIndex].Value; /// my delete logic CTX.SubmitChanges(); gv1.DataBind(); } 
+4
source share
3 answers

If your record is really deleted correctly and the grid just doesn’t display the changes, I would try putting the GridView inside the UpdatePanel and see if that helps.

 <asp:UpdatePanel runat="server" UpdateMode="Always"> <ContentTemplate> // GridView </ContentTemplate> </asp:UpdatePanel> 
+1
source

I cannot point out an error in your code, but I give you my code, which I made to delete the data.

 <asp:GridView ID="GridView1" runat="server" OnRowDeleting="Del" DataKeyNames="Clg_ID" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2"> <Columns> <asp:CommandField ShowDeleteButton="True" /> </Columns> <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#FFF1D4" /> <SortedAscendingHeaderStyle BackColor="#B95C30" /> <SortedDescendingCellStyle BackColor="#F1E5CE" /> <SortedDescendingHeaderStyle BackColor="#93451F" /> </asp:GridView> 

.cs file

 protected void Del(object sender, GridViewDeleteEventArgs e) { string i = GridView1.DataKeys[0].Value.ToString(); SqlConnection cn = new SqlConnection(); cn.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString.ToString(); cn.Open(); SqlCommand cmd = new SqlCommand("Delete from Feedback where Clg_ID='" + i + "'", cn); cmd.ExecuteNonQuery(); cmd.Dispose(); cn.Close(); Response.Redirect("MFeedback.aspx"); } 
+1
source

Try setting the EditIndex GridView to -1 in the ItemDeleting GridView method:

 protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e) { gv1.EditIndex = -1; gv1.DataBind(); } 

and transferring your deletion logic to the RowCommand GridView method:

 protected void gv1_RowCommand(object sender, GridViewDeleteEventArgs e) { If (e.CommandName == "Delete") { int instructionId = (int) gv1.DataKeys[e.RowIndex].Value; /// my delete logic CTX.SubmitChanges(); } } 

Your control for the delete action should look something like this:

 <asp:LinkButton ID="lbDel" runat="server" Text="Delete" CommandName="Delete" /> 
0
source

Source: https://habr.com/ru/post/1444957/


All Articles