Total Pageviews

2 May 2012

How delete check box wise in Grid view data ASP.net

Default.aspx=>
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
     

</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
         <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
             AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="CustomerID"
             Width="41px" CellPadding="4" ForeColor="#333333" GridLines="None"
             onselectedindexchanged="GridView1_SelectedIndexChanged" Height="32px">   
        
               <Columns>         
                       <asp:CommandField ShowSelectButton="True" />
                                         <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" InsertVisible="False" ReadOnly="True" /> 
                                                         <asp:BoundField DataField="Firstname" HeaderText="Firstname"  />                 
                                                         <asp:TemplateField HeaderText="Select">
                                                                               <ItemTemplate>   
                                                                                                     
                                                                                                     
                                         <asp:CheckBox ID="chkSelect" runat="server" />    </ItemTemplate>  <HeaderTemplate>
                                        <%--<input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server" type="checkbox" />--%>     
                                         
                                                     </HeaderTemplate>         
                                                             </asp:TemplateField>       
                                                                  </Columns>   
                                                                       </asp:GridView>       
<br />
<br />        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Delete" /> 
       <asp:Label ID="lblmsg" runat="server" Text="Label" Visible="false"></asp:Label>
</asp:Content>
Default.aspx.cs=>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString()); 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
    public void BindGridView()
    {
        SqlDataAdapter ad = new SqlDataAdapter("select * from tbl_CustomerDetail order by CustomerID", con);
        DataSet ds = new DataSet(); ad.Fill(ds);
        GridView1.DataSource = ds; GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = ((CheckBox)(row.FindControl("chkSelect")));
            if (cb != null && cb.Checked)
            {
                int custID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                string sqlstr = "delete from tbl_CustomerDetail where CustomerID='" + custID + "'"; SqlCommand mycmd;
                mycmd = new SqlCommand(sqlstr, con);
                con.Open();
                mycmd.ExecuteNonQuery();
                BindGridView();
            }
            con.Close();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString()); 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }
    public void BindGridView()
    {
        SqlDataAdapter ad = new SqlDataAdapter("select * from tbl_CustomerDetail order by CustomerID", con);
        DataSet ds = new DataSet(); ad.Fill(ds);
        GridView1.DataSource = ds; GridView1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cb = ((CheckBox)(row.FindControl("chkSelect")));
            if (cb != null && cb.Checked)
            {
                int custID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                string sqlstr = "delete from tbl_CustomerDetail where CustomerID='" + custID + "'"; SqlCommand mycmd;
                mycmd = new SqlCommand(sqlstr, con);
                con.Open();
                mycmd.ExecuteNonQuery();
                BindGridView();
            }
            con.Close();
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
}

No comments:

Post a Comment

Contact Form

Name

Email *

Message *