Click here to Skip to main content
15,917,565 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can I add a checkbox column in an ASP.NET GridView so that I can check a row and hit a button to delete it?
Posted
Comments
I.explore.code 13-Oct-11 13:51pm    
well, you did copy what I wrote as your question, but did you try that link that i had posted along with it? :)

for selecting all the check boxes in gridview you can use javascript like this


<pre lang="cs">

function SelectAllCheckboxes(spanChk)
{
var oItem = spanChk.children;
var theBox= (spanChk.type==&quot;checkbox&quot;) ?
spanChk : spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;

for(i=0;i&lt;elm.length;i++)
if(elm[i].type==&quot;checkbox&quot; &amp;&amp;
elm[i].id!=theBox.id)
{
if(elm[i].checked!=xState)
elm[i].click();
}
}


</pre>



this is the gridview with checkbox column ::-

XML
<asp:GridView ID="gvCity" runat="server" AutoGenerateColumns="False" Width="99%"
                                    OnRowCommand="gvCity_RowCommand" OnRowEditing="gvCity_RowEditing" OnRowDeleting="gvCity_RowDeleting"
                                    AllowPaging="True" OnPageIndexChanging="gvCity_PageIndexChanging" CssClass="allgrids"
                                    HeaderStyle-BackColor="#ededec">
                                    <PagerSettings FirstPageText="First" NextPageText="Next" PreviousPageText="Previous"
                                        LastPageText="Last" />
                                    <PagerStyle HorizontalAlign="Right" />
                                    <Columns>
                                        <asp:TemplateField>
                                            <HeaderTemplate>
                                                <input id="chkAll" onclick="javascript:SelectAllCheckboxes(this);" runat="server"
                                                    type="checkbox" />
                                            </HeaderTemplate>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="chkSelect" runat="server" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="City">
                                            <ItemTemplate>
                                                <asp:Label ID="lblcity" runat="server" Text='<%#Eval("CITY_NAME") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="State">
                                            <ItemTemplate>
                                                <asp:Label ID="lblState" runat="server" Text="Andhra Pradesh" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Country">
                                            <ItemTemplate>
                                                <asp:Label ID="lblCountry" runat="server" Text="India" />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Date Of Creation">
                                            <ItemTemplate>
                                                <asp:Label ID="lblDate" runat="server" Text='<%#Eval("Date") %>' />
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Actions">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="lbtnEdit" runat="server" Text="Edit" ForeColor="Blue" CommandName="Edit"
                                                    CommandArgument='<%#Eval("CITY_ID") %>' />
                                                <asp:LinkButton ID="lbtnDelete" runat="server" Text="Delete" ForeColor="Blue" CommandName="Delete"
                                                    CommandArgument='<%#Eval("CITY_ID") %>' OnClientClick="return confirm('Are you sure you want to delete ?');" />

                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>
 
Share this answer
 
 
Share this answer
 
v2
Hi,

I 've some sample code for your requirement.

check this once

ASP.NET
 <asp:datalist id="DataList1" runat="server" width="400" xmlns:asp="#unknown">
 <HeaderTemplate >
   <table width="100%" align="center">
      <tr>
        <td>ID</td>
        <td>Name</td>
        <td>Operation</td>
      </tr>

 </HeaderTemplate>
 <itemtemplate>
      <tr>
        <td>
            <asp:label id="Label1" runat="server" text="<%#Eval("id") %>"></asp:label></td>
        <td>
            <asp:label id="Label2" runat="server" text="<%#Eval("name") %>"></asp:label></td>
        <td>
            <asp:checkbox id="CheckBox1" runat="server" /></td>
      </tr>
 </itemtemplate>
 <footertemplate>
  </footertemplate></table>

</asp:datalist>


And write following code in button click event

C#
strin cnt=String.Empty;

foreach (DataListItem dti in DataList1.Items)
{
    if (((CheckBox)dti.FindControl("CheckBox1")).Checked)
    {
         if(cnt.Length>0)
         {
           cnt=cnt+","+((Label)dti.FindControl("Label1")).Text ;
          }
          else
          {
          cnt=((Label)dti.FindControl("Label1")).Text ;
          }
    }
}


in the above code you'll get all ids which are checked in data control.

By using those you can write your sql query for delete them

your query just like...

C#
string qury="delete from <tablename> where id in ("+cnt+")";
</tablename>


then you can achieve your requirement.

All the Best
 
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