Below Example is very useful for How to delete a row in gridview . Let me know In case you are facing any problem
Design :
<asp:GridView ID="gvNew" runat="server" AutoGenerateColumns="false" OnRowCancelingEdit="gvNew_RowCancelingEdit"
OnRowCommand="gvNew_RowCommand" OnRowDataBound="gvNew_RowDataBound" OnRowEditing="gvNew_RowEditing"
OnRowUpdated="gvNew_RowUpdated" OnRowUpdating="gvNew_RowUpdating" AllowPaging="true"
PageSize="30" DataKeyNames="ecm_id" OnPageIndexChanging="gvNew_PageIndexChanging"
ShowFooter="false" OnRowDeleting="gvNew_RowDeleting">
<Columns>
<asp:TemplateField HeaderStyle-Height="30px" FooterStyle-Height="25px">
<HeaderTemplate>
<asp:LinkButton ID="lnkECM_ID" runat="server" Text="ECM Number" CommandName="ECM_ID"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ecm_id") %>' ID="lblECM_id" Width="100px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Height="30px" FooterStyle-Height="25px">
<HeaderTemplate>
<asp:LinkButton ID="lnkECM" runat="server" Text="ECM Description" CommandName="ECM"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ECM") %>' ID="lblECM" Width="240px"></asp:Label>
</ItemTemplate>
<asp:TemplateField HeaderText="Action" HeaderStyle-Height="30px" FooterStyle-Height="25px">
<ItemTemplate>
<asp:Button Text="Delete" ID="btnDelete"
runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Delete"
ToolTip="Delete" />
</ItemTemplate>
</asp:TemplateField>
</asp:TemplateField>
</Columns>
</ asp:GridView>
Code :
protected void gvNew_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int deleteRowID = Convert.ToInt32(e.CommandArgument);
int ecmid = Convert.ToInt32(gvNew.DataKeys[(deleteRowID) - (this.gvNew.PageIndex * 30)].Value.ToString());
ObjAtSite.ecm_id = Convert.ToInt32(ecmid);
ObjAtSite.p_id = Convert.ToInt32(ddlProperty.SelectedValue);
ds = ObjAtSite.DeleteECMData();
if (ds.Tables[0].Rows[0][0].ToString() == "3")
{
gvNew.EditIndex = -1;
// In this method you can write code for GridView data bind
BindGridDataAll();
//string message = "Data deleted successfully";
//string script = "<script language=\"javascript\" type=\"text/javascript\">;alert('" + message + "');</script>";
//ScriptManager.RegisterStartupScript(Page, this.GetType(), "AlertMessage", script, false);
}
}
}
Design :
<asp:GridView ID="gvNew" runat="server" AutoGenerateColumns="false" OnRowCancelingEdit="gvNew_RowCancelingEdit"
OnRowCommand="gvNew_RowCommand" OnRowDataBound="gvNew_RowDataBound" OnRowEditing="gvNew_RowEditing"
OnRowUpdated="gvNew_RowUpdated" OnRowUpdating="gvNew_RowUpdating" AllowPaging="true"
PageSize="30" DataKeyNames="ecm_id" OnPageIndexChanging="gvNew_PageIndexChanging"
ShowFooter="false" OnRowDeleting="gvNew_RowDeleting">
<Columns>
<asp:TemplateField HeaderStyle-Height="30px" FooterStyle-Height="25px">
<HeaderTemplate>
<asp:LinkButton ID="lnkECM_ID" runat="server" Text="ECM Number" CommandName="ECM_ID"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ecm_id") %>' ID="lblECM_id" Width="100px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Height="30px" FooterStyle-Height="25px">
<HeaderTemplate>
<asp:LinkButton ID="lnkECM" runat="server" Text="ECM Description" CommandName="ECM"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ECM") %>' ID="lblECM" Width="240px"></asp:Label>
</ItemTemplate>
<asp:TemplateField HeaderText="Action" HeaderStyle-Height="30px" FooterStyle-Height="25px">
<ItemTemplate>
<asp:Button Text="Delete" ID="btnDelete"
runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Delete"
ToolTip="Delete" />
</ItemTemplate>
</asp:TemplateField>
</asp:TemplateField>
</Columns>
</ asp:GridView>
Code :
protected void gvNew_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int deleteRowID = Convert.ToInt32(e.CommandArgument);
int ecmid = Convert.ToInt32(gvNew.DataKeys[(deleteRowID) - (this.gvNew.PageIndex * 30)].Value.ToString());
ObjAtSite.ecm_id = Convert.ToInt32(ecmid);
ObjAtSite.p_id = Convert.ToInt32(ddlProperty.SelectedValue);
ds = ObjAtSite.DeleteECMData();
if (ds.Tables[0].Rows[0][0].ToString() == "3")
{
gvNew.EditIndex = -1;
// In this method you can write code for GridView data bind
BindGridDataAll();
//string message = "Data deleted successfully";
//string script = "<script language=\"javascript\" type=\"text/javascript\">;alert('" + message + "');</script>";
//ScriptManager.RegisterStartupScript(Page, this.GetType(), "AlertMessage", script, false);
}
}
}
No comments:
Post a Comment