Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
=======================

XML
<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
Comments
RDBurmon 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.

 
Share this answer
 
Comments
Vani Kulkarni 12-Jun-12 7:11am    
Good Link!
Prasad_Kulkarni 12-Jun-12 7:36am    
Thank you Vani!
VJ Reddy 12-Jun-12 11:18am    
Good references. 5!
Prasad_Kulkarni 12-Jun-12 23:46pm    
Thank you VJ!
Add Grid View Like this

XML
<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


C#
<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
VB
<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

}
}
 
Share this answer
 
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900