Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2005 > SQL Server 2005
| Search | Today's Posts | Mark Forums Read
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 6th, 2007, 02:49 AM
Registered User
Join Date: Apr 2006
Location: new delhi, new delhi, India.
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to deepakbachelor
Default Deleting multiple rows in data view or grid

<script language="javascript" type="text/javascript">
    function select_deselectAll (chkVal, idVal)
    var frm = document.forms[0];
    // Loop through all elements
    for (i=0; i<frm.length; i++)
        // Look for our Header Template's Checkbox
        if (idVal.indexOf ('CheckAll') != -1)
            // Check if main checkbox is checked, then select or deselect datagrid checkboxes
            if(chkVal == true)
                frm.elements[i].checked = true;
                frm.elements[i].checked = false;
            // Work here with the Item Template's multiple checkboxes
        else if (idVal.indexOf ('DeleteThis') != -1)
            // Check if any of the checkboxes are not checked, and then uncheck top select all checkbox
            if(frm.elements[i].checked == false)
                frm.elements[1].checked = false; //Uncheck main select all checkbox

function confirmDelete (frm)
    // loop through all elements
    for (i=0; i<frm.length; i++)
        // Look for our checkboxes only
        if (frm.elements[i].name.indexOf("DeleteThis") !=-1)
            // If any are checked then confirm alert, otherwise nothing happens
                return confirm ('Are you sure you want to delete your selection(s)?')
<asp:GridView ID="DgAddress" runat="server" PageSize="40" Width="278px" AutoGenerateColumns="False">
            <PagerStyle BackColor="#C000C0" />
            <AlternatingRowStyle BackColor="#C0C0FF" BorderColor="#C0C0FF" BorderStyle="Solid"
                ForeColor="#8080FF" />
                <asp:Button ID="BtnGrid" runat="server" Text="Delete"/>
                <asp:BoundField HeaderText="ID" DataField="Id" Visible="false" />
                <asp:BoundField HeaderText="Employee Id" DataField="EmpId"/>
                <asp:BoundField HeaderText="Employee Name" DataField="EmpName"/>
                <asp:CommandField InsertText="Delete" ShowDeleteButton="True"/>
                <asp:CheckBox Id="CheckAll" onClick="javascript: return select_deselectAll (this.checked, this.id);" runat=server/>
                <asp:HiddenField ID="chkid" runat="server" Value=<%#Container.DataItem("Id")%>/>
                <asp:CheckBox Id="DeleteThis" onClick="javascript: return select_deselectAll (this.checked, this.id);" runat=server/>

<asp:Button Text="Delete Items" ID="Confirm" runat="server" />

Protected Sub Confirm_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Confirm.Click
        Dim dgIDs As String = ""
        Dim BxsChkd As Boolean
        Dim i As Integer
        Dim deleteChkBxItem As CheckBox
        Dim sqlconn As New SqlConnection("Initial Catalog=master;Data Source=tcs139054;user id=sa;password=kolkata@1")
        BxsChkd = False
        i = 2
        For Each gv As GridViewRow In DgAddress.Rows
            deleteChkBxItem = gv.FindControl("DeleteThis")
            If deleteChkBxItem.Checked = True Then
                BxsChkd = True
                dgIDs += CType(gv.FindControl("chkid"), HiddenField).Value.ToString + ","
            End If
            i += 1
        If BxsChkd Then
                Dim deleteSQL As String = "delete from employee where Id IN (" & dgIDs.Substring(0, dgIDs.LastIndexOf(",")) & ")"
                Dim cmd As SqlCommand = New SqlCommand(deleteSQL, sqlconn)
            Catch Err As SqlException
            End Try
        End If
    End Sub
 Private Sub FillGrid()
        Dim sqlconn As New SqlConnection("Initial Catalog=master;Data Source=tcs139054;user id=sa;password=kolkata@1")
        Dim sqlcmd As New SqlCommand
        Dim sqldr As SqlDataReader
        sqlcmd.Connection = sqlconn
        sqlcmd.CommandType = Data.CommandType.Text
        sqlcmd.CommandText = "select id,empid,empname from employee"
        sqldr = sqlcmd.ExecuteReader
        DgAddress.DataSource = sqldr
    End Sub

Deepak Kumar Sharma
PGMS Inc(Noida)(India)
Cell No:-9719395072/9313107703
Old December 7th, 2007, 02:19 PM
Friend of Wrox
Join Date: Aug 2006
Location: Bangalore, Karnataka, India.
Posts: 231
Thanks: 0
Thanked 1 Time in 1 Post

is it is your solution or you have some doubt so that i should read code.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add Rows to Grid View dpkbahuguna ASP.NET 1.x and 2.0 Application Design 0 March 19th, 2008 04:05 AM
Paging Data grid view Prakash001 Visual Basic 2005 Basics 0 February 6th, 2008 04:07 AM
Deleting rows from a data table in datagrid ann03 ASP.NET 1.x and 2.0 Application Design 1 October 23rd, 2007 06:23 AM
Data Grid View CoolDude C# 2005 3 March 10th, 2007 05:37 AM
Deleting Rows in an Editable Data Grid RichardP ASP.NET 1.0 and 1.1 Professional 2 March 1st, 2006 02:19 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.