multi delete in gridview...

Gopal_nivas
Posted by Gopal_nivas under ASP.NET category on | Views : 2561
<script language="javascript" type="text/javascript">

function ConfirmDelete()

{

return confirm("Are u sure u want to delete all these records?")

}

</script>



<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"

AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="CustomerID">

<Columns>

<asp:TemplateField>

<ItemTemplate>

<asp:CheckBox ID="chkgrid" runat="server"/>

</ItemTemplate>

</asp:TemplateField>

<asp:BoundField DataField="CustomerID" Visible="false"/>

<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />

<asp:BoundField DataField="ContactName" HeaderText="ContactName" SortExpression="ContactName" />

<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />

</Columns>

</asp:GridView>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbPDMS_V1.5ConnectionString6 %>"

SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [Country] FROM [Customer]" DeleteCommand="Delete from [Customer] where [CustomerID]=@Customerid">

<DeleteParameters>

<asp:Parameter Name="CustomerID"/>

</DeleteParameters>

</asp:SqlDataSource>

<asp:Button ID="btndelete" Text="delete" runat="server" OnClick="btndelete_click" OnClientClick="return ConfirmDelete()"/>



code behind:

using System;

using System.Data;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

public partial class batchDelete : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void btndelete_click(object sender ,EventArgs e)

{

foreach(GridViewRow row in GridView1. Rows)

{

CheckBox cb = (CheckBox)row.FindControl("chkgrid");

if(cb.Checked)

{

int customerid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

SqlDataSource1.DeleteParameters["CustomerID"].DefaultValue = customerid.ToString();

SqlDataSource1.Delete();

}

}

}

}

Comments or Responses

Login to post response