Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET SQL-Server
Dear Friends,
 
Am working on C# , asp.net , SqlServer 2005.
 
I have a gridview with data filled in it.
 
am using Header and row by row checkboxes to delete single and multiple records.
 
this is my design page.
=======================
 
<Columns>
 
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:CheckBox ID="Checkall" runat="server"  onclick="javascript:SelectAll(this);" />
                                    </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="Chkselect" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
                <asp:BoundField DataField="UserName" HeaderText="UserName"
                    SortExpression="UserName" />
                <asp:BoundField DataField="Login_Time" HeaderText="Login_Time"
                    SortExpression="Login_Time" />
                <asp:BoundField DataField="Logout_Time" HeaderText="Logout_Time"
                    SortExpression="Logout_Time" />
            </Columns>
 

Please can anybody help me, how to delete the records by selecting checkboxes
 
I need All records to be deleted and multiple deletion, and single deletion.
 
Please help me
 
Thanks.
Posted 12-Jun-12 2:08am
Comments
RDBurmon at 13-Jun-12 9:34am
   
Thanks Everyone who replied to this thread , So Ranjith, I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
Vani Kulkarni at 12-Jun-12 7:11am
   
Good Link!
Prasad_Kulkarni at 12-Jun-12 7:36am
   
Thank you Vani!
VJ Reddy at 12-Jun-12 11:18am
   
Good references. 5!
Prasad_Kulkarni at 12-Jun-12 23:46pm
   
Thank you VJ!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Add Grid View Like this
 
<asp:GridView ID="GVID">
 <Columns>
<asp:TemplateField HeaderText="Select">
                                                                                                   <ItemTemplate>
                                                                                                       <asp:CheckBox ID="QchkBxSelect" runat="server" />
                                                                                                   </ItemTemplate>
                                                                                                   <HeaderTemplate>
                                                                                                       <asp:CheckBox ID="QchkBxHeader" onclick="javascript:QHeaderClick(this);" runat="server" />
                                                                                                   </HeaderTemplate>
                                                                                               </asp:TemplateField>
<asp:TemplateField HeaderText="Risk ID" meta:resourcekey="asptempriskid">
                                                                                                   <ItemTemplate>
                                                                                                       <asp:Label ID="lbluniqueId" Text='<%# Eval("uniqueID") %>' runat="server"></asp:Label>
                                                                                                   </ItemTemplate>
                                                                                               </asp:TemplateField>
 
</Columns>
                                                                                                                   </asp:GridView>
 

 
Call The Java Script to Select DeSelect Checkbox
 

<script type="text/javascript">
var QTotalChkBx;
        var QCounter;
 

        window.onload = function () {
            //Get total no. of CheckBoxes in side the GridView.

            QTotalChkBx = parseInt('<%= this.GVID.Rows.Count %>');
 
            //Get total no. of checked CheckBoxes in side the GridView.

            QCounter = 0;
        }
function QHeaderClick(QCheckBox) {
            //Get target base & child control.
            var QTargetBaseControl =
       document.getElementById('<%= this.GVID.ClientID %>');
            var QTargetChildControl = "QchkBxSelect";
 
            //Get all the control of the type INPUT in the base control.
            var QInputs = QTargetBaseControl.getElementsByTagName("input");
 
            //Checked/Unchecked all the checkBoxes in side the GridView.
            for (var n = 0; n < QInputs.length; ++n)
                if (QInputs[n].type == 'checkbox' &&
                QInputs[n].id.indexOf(QTargetChildControl, 0) >= 0)
                    QInputs[n].checked = QCheckBox.checked;
 
            //Reset Counter
            QCounter = QCheckBox.checked ? QTotalChkBx : 0;
        }
 
        function QChildClick(QCheckBox, QHCheckBox) {
            //get target control.
            var QHeaderCheckBox = document.getElementById(QHCheckBox);
 
            //Modifiy Counter;
            if (QCheckBox.checked && QCounter < QTotalChkBx)
                QCounter++;
            else if (QCounter > 0)
                QCounter--;
 
            //Change state of the header CheckBox.
            if (QCounter < QTotalChkBx)
                QHeaderCheckBox.checked = false;
            else if (QCounter == QTotalChkBx)
                QHeaderCheckBox.checked = true;
        }
 

    </script>
add Asp Button Outside Grid View Like this
<asp:Button ID="Delete" runat="server" Text="Delete "
                                                                                            OnClick="Delete_Click" />
 
Try this below Code In Delete Button onClick Event
 
string flagchecked = "0";
ArrayList arrrid = new ArrayList();
// Select the checkboxes from the GridView control
for (int i = 0; i < GVID.Rows.Count; i++)
{
GridViewRow row = GVID.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkBxSelect")).Checked;
 
if (isChecked)
{
flagchecked = "1";
string uniqueid= ((Label)row.FindControl("lblitemuniqueId")).Text;
arrrid.Add(uniqueid);
}
}
if (flagchecked == "1")
{
for (int jR = 0; jR < arrrid.Count; jR++)
{
//Delete Operation
 
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.141223.1 | Last Updated 12 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100