Saturday 17 July 2010

GridView Multiple Delete with CheckBox and Confirm

Multiple Deletes in Gridview


Code For Default.aspx Page


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
       
        <tr>
            <td colspan="3">
                <asp:Panel ID="Panel2" runat="server" Height="550px" Width="800px" BorderColor="Navy" BorderStyle="Solid" BorderWidth="1px" Font-Bold="True" ScrollBars="Both">
                    <table >
                      
                        <tr>
                            <td colspan="3" style="height: 141px" align="left" valign="top">
                           
                                    <asp:GridView ID="GridView_usedCar" runat="server" CellPadding="4"
                                        ForeColor="#333333" GridLines="None"
                                    Width="726px" AutoGenerateColumns="False" AllowPaging="True"
                                        AllowSorting="True" Height="380px"                                     
                                     
                                     
                                      
                                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                    <EditRowStyle BackColor="#999999"/>
                                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" Font-Names="Arial Black" VerticalAlign="Top" />
                                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                    <Columns>
                                     <asp:TemplateField>
                                        <HeaderTemplate>
                                            <asp:LinkButton ID="lbtndelete" runat="server" CommandName="Delete" Font-Bold="True"
                                                ForeColor="Firebrick" Height="27px" OnClick="lbtndelete_Click" OnClientClick="javascript :return confirm('Are you sure ,you want to delete selected records ?');"
                                                Text="Delete" Width="49px" Font-Size="11pt" BorderColor="White"></asp:LinkButton>
                                        </HeaderTemplate>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="cbusedcarId" runat="server" TextAlign="Left" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    
                                    <asp:TemplateField HeaderText="Sell CarID"  Visible="False">
                                        <ItemTemplate>
                                            <asp:Label ID="lblSellCarID" runat="server" Text='<% #Eval("SellCarID")%>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                   
                                    <asp:TemplateField HeaderText="Car Name">
                                      <ItemTemplate>
                                      <asp:Label ID="lblCarName" runat="server" Text='<% #Eval("CarName")%>'></asp:Label>
                                      </ItemTemplate>
                                     </asp:TemplateField>
                                    
                                      <asp:TemplateField HeaderText="Car Model">
                                      <ItemTemplate>
                                      <asp:Label ID="lblCarModel" runat="server" Text='<% #Eval("CarMode")%>'></asp:Label>
                                      </ItemTemplate>
                                     </asp:TemplateField>
                                     
                                      <asp:TemplateField HeaderText="User Name">
                                       <EditItemTemplate>
                                        <asp:TextBox ID="userNametxt" runat="server" Text='<% #Eval("Name")%>'
                                            Width="70"></asp:TextBox>
                                       </EditItemTemplate>
                                       <ItemTemplate>
                                        <asp:Label ID="lblName" runat="server" Text='<% #Eval("Name")%>'></asp:Label>
                                      </ItemTemplate>
                                    </asp:TemplateField>                                 
                                   
                                    <ItemTemplate>
                                        <asp:LinkButton ID="lbtnedit" runat="server" CommandName="Edit" Font-Bold="True"
                                            Font-Size="10pt" Height="16px" Text="Edit" Width="25px"></asp:LinkButton>
                                    </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                        <PagerSettings Position="TopAndBottom" />
                                </asp:GridView>
                              
                            </td>
                        </tr>
                    </table>
                </asp:Panel>
            </td>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>


Code For Default.aspx.cs Page

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;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Collections.Specialized;

public partial class Default2 : System.Web.UI.Page
{
    static int flag;
    Connection cn = new Connection();
    SqlConnection con;
    DataSet ds = new DataSet();
    GetData g = new GetData();
    protected void Page_Load(object sender, EventArgs e)
    {
        FillDD ddl = new FillDD();
        if (!IsPostBack)
        {
            ddl.FillDropdownCar(ref ddl_fltrbycar, "SELECT Distinct CarName FROM dbo.vw_OldCarDetail");
            BindGrid("SELECT SellCarID, CarName, CarMode, Name FROM dbo.vw_OldCarDetail");
        }

    }

    public void BindGrid(string Query)
    {
        con = cn.ConnCar();
        try
        {
            ds.Clear();
            if (con.State == ConnectionState.Closed)
                con.Open();

            ds = g.GetdatasetCar(Query);
            GridView_usedCar.DataSource = ds;
            GridView_usedCar.DataBind();
            lbl_oldcar.Text = "Total" + " "+ ds.Tables[0].Rows.Count + "" + "Old Car Found.............";

        }
        catch (Exception)
        {
            Response.Write("<script language='javascript'>alert('Sorry!!Could Not Connect to Server >>???');{ window.close();}</script>");

        }

        finally
        {
            con.Close();
        }
    }
  
   
    protected void lbtndelete_Click(object sender, EventArgs e)
    {
        StringCollection sc = new StringCollection();
        string id = string.Empty;

        for (int i = 0; i <GridView_usedCar.Rows.Count; i++)//loop the GridView Rows
        {
            CheckBox cb = (CheckBox)GridView_usedCar.Rows[i].Cells[0].FindControl("cbusedcarId"); //find the CheckBox
            if (cb != null)
            {
                if (cb.Checked)
                {
                    Label lbl_id = (Label)(GridView_usedCar.Rows[i].Cells[1].FindControl("lblSellCarID")) as Label;// get the id of the field to be deleted
                    id = lbl_id.Text;
                    sc.Add(id); // add the id to be deleted in the StringCollection
                }
            }
        }
        if (id != "")
            DeleteRecords(sc);
        else
            message("Plz Select Record to delete???");

        if (flag == 1)
            BindGrid("SELECT SellCarID, CarName, CarMode, Name, Email, Price, Kilometers FROM dbo.vw_OldCarDetail where CarName='" + ddl_fltrbycar.SelectedValue + "'");
        if (flag == 0)
            BindGrid("SELECT SellCarID, CarName, CarMode, Name, Email, Price, Kilometers FROM dbo.vw_OldCarDetail");

    }
    #region Multiple Delete
    private void DeleteRecords(StringCollection sc)
    {
        con = cn.ConnCar();
        StringBuilder sb = new StringBuilder(string.Empty);
        Magic m_del = new Magic();
        string sellcar_id = "";
        foreach (string item in sc)
        {
            sellcar_id += item.ToString() + ",";
        }
        try
        {
            con.Open();
            string strIDs = sellcar_id.Substring(0, sellcar_id.LastIndexOf(","));
            string sqlstr_del = "DELETE FROM tbl_trn_SellCar WHERE  SellCarID in (" + strIDs + ")";
            int i = m_del.AllperformCar(sqlstr_del);
            if (i > 0)
            {
                message("Select Record Deleted !!");
            }
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Deletion Error:";
            msg += ex.Message;
            throw new Exception(msg);

        }
        finally
        {
            con.Close();
        }
    #endregion

    }
   
}



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger