I have used below code for delete command of GridView, but when I press delete command , the data in grid view will not be refreshed, actually command deletes the row but grid view does not update. I have to refresh manually browser then grid view will be update.
Markup:
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridViewDocuments_Search" runat="server" AutoGenerateColumns=False
Visible="False" onrowcommand="GridViewDocuments_Search_RowCommand"
DataKeyNames="DocID" PageSize="100" EnableSortingAndPagingCallbacks="True" >
<Columns>
<asp:TemplateField HeaderText = "Details">
<ItemTemplate>
<asp:Button ID ="btn_Show" Text="Details" runat= "server" CommandName= "Details" CommandArgument='<%#
Container.DataItemIndex%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DocumentNo" HeaderText="DocNo" />
<asp:BoundField DataField="title" HeaderText="Title" SortExpression="title" />
<asp:BoundField DataField="Docid" HeaderText="Docid" Visible="false" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
The code:
protected void GridViewDocuments_Search_RowCommand(object sender, GridViewCommandEventArgs e)
{
int rowindex = Convert.ToInt32(e.CommandArgument.ToString());
_DataContext = new EDMSDataContext();
int _Docid = (int)GridViewDocuments_Search.DataKeys[rowindex].Value;
switch (e.CommandName)
{
case "Details":
Response.Redirect("~/Documentfortest.aspx?DocID=" + _Docid);
break;
case "Delete":
_DataContext.DeleteDoc(_Docid);
break;
}
var query = _DataContext.spQuickSearchDoc(txtSearchKeywords.Text);
GridViewDocuments_Search.DataSource = query;
GridViewDocuments_Search.DataBind();
}
Make sure that you re-assign the
DataSourceproperty before you callDataBindand then try ?