Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
delete button:
C#
for (int i = dt.Rows.Count - 1; i ;= 0; i--)
         {
             CheckBox ck = (CheckBox)grdbilling.Rows[i].Cells[0].FindControl(chkbox);
             if (ck.Checked == true)
             {
                 dt.Rows[i].Delete();
                 grdbilling.DataSource = dt;
                 grdbilling.DataBind();
                 lblDelete.Visible = true;
                 lblDelete.Text =Select Records Deleted.;
             }

         }

I tried this above code.
it allow me to delete only one record in gridview binded to datatable with no database related.

help me guys.
Posted
Updated 3-Jun-12 23:29pm
v2
Comments
The Doer 4-Jun-12 5:31am    
what u want?? do you want to delete it from database too??

hi,
you are deleting the row from the data table but you are not updating the data in the database.without updating the database it won't make any changes to database data
 
Share this answer
 
 
Share this answer
 
Please refer answer for similar question,
Delete rows from datgridview and database access[^]
You will get some help from it, for sure.
 
Share this answer
 
Grid View add Checkbox like below

XML
<asp:TemplateField HeaderText="Select">
                                                                                            <ItemTemplate>
                                                                                                <asp:CheckBox ID="chkBxSelect" runat="server" />
                                                                                            </ItemTemplate>
                                                                                            <HeaderTemplate>
                                                                                                <asp:CheckBox ID="chkBxHeader" onclick="javascript:HeaderClick(this);" runat="server" />
                                                                                            </HeaderTemplate>
                                                                                        </asp:TemplateField>

HTML


C#
<script type="text/javascript">
        var TotalChkBx;
        var Counter;
        

        window.onload = function () {
            //Get total no. of CheckBoxes in side the GridView.
            TotalChkBx = parseInt('<%= this.GridviewID.Rows.Count %>');
           

            //Get total no. of checked CheckBoxes in side the GridView.
            Counter = 0;
           
        }

        function HeaderClick(CheckBox) {
            //Get target base & child control.
            var TargetBaseControl =
       document.getElementById('<%= this.GridviewID.ClientID %>');
            var TargetChildControl = "chkBxSelect";

            //Get all the control of the type INPUT in the base control.
            var Inputs = TargetBaseControl.getElementsByTagName("input");

            //Checked/Unchecked all the checkBoxes in side the GridView.
            for (var n = 0; n < Inputs.length; ++n)
                if (Inputs[n].type == 'checkbox' &&
                Inputs[n].id.indexOf(TargetChildControl, 0) >= 0)
                    Inputs[n].checked = CheckBox.checked;

            //Reset Counter
            Counter = CheckBox.checked ? TotalChkBx : 0;
        }

        function ChildClick(CheckBox, HCheckBox) {
            //get target control.
            var HeaderCheckBox = document.getElementById(HCheckBox);

            //Modifiy Counter;
            if (CheckBox.checked && Counter < TotalChkBx)
                Counter++;
            else if (Counter > 0)
                Counter--;

            //Change state of the header CheckBox.
            if (Counter < TotalChkBx)
                HeaderCheckBox.checked = false;
            else if (Counter == TotalChkBx)
                HeaderCheckBox.checked = true;
        }


Insert Delete delete button for gridview outside
<asp:button id="btnDelete" runat="server" text="Delete" onclick="btnDelete_Click" xmlns:asp="#unknown">

string flagchecked = "0";
ArrayList arrriskid = new ArrayList();
// Select the checkboxes from the GridView control
for (int i = 0; i < GridviewID.Rows.Count; i++)
{
GridViewRow row = GridviewID.Rows[i];
bool isChecked = ((CheckBox)row.FindControl("chkBxSelect")).Checked;

if (isChecked)
{
flagchecked = "1";
string deleteid= ((Label)row.FindControl("itemteplatedeleteid")).Text;
arrriskid.Add(deleteid);
}
}
if (flagchecked == "1")
{
for (int jR = 0; jR < arrriskid.Count; jR++)
{
dt_Datatable = (DataTable)Session["dt_Datatable "];
if (dt_Datatable .Rows.Count > 0)
{
//use loop must reverse order its may helps to avoide error when deleting rows in dt_Datatable
int loopcount = dt_Datatable .Rows.Count - 1;
for (int j = loopcount; j >= 0; j--)
{
if (dt_Datatable.Rows[j]["DeleteID"].ToString() == arrriskid[jR].ToString())
{

dt_Datatable .Rows[j].Delete();
dt_Datatable .AcceptChanges();
}
}

}

Then Last bind gridview again from Datatable

C#
G<pre lang="xml">GridviewID.DataSource = dt_<pre lang="xml">Datatable ;
               GridviewID.DataBind();&lt;/pre&gt;</pre>
 
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