Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: ASP.NET4.0
Dear Experts,

Please can u suggest me Code..for Deleting records from Gridview using Checkbox in
I do not know what is the row index
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
      onselectedindexchanging="GridView1_SelectedIndexChanging" Width="26px" 
        onrowcreated="GridView1_RowCreated"  >
         &lt;asp:CheckBox  runat="server" ID="CheckBoxItem"   /&gt;
        &lt;asp:BoundField DataField="Username" HeaderText="Username" /&gt;
        &lt;asp:BoundField DataField="VideoTitle" HeaderText="VideoTitle" /&gt;
        &lt;asp:BoundField DataField="LineDescription" HeaderText="Comment" /&gt;
        &lt;asp:BoundField DataField="RegDate"  HeaderText="RegDate"/&gt;
        &lt;asp:BoundField DataField="RegTime" HeaderText="RegTime" /&gt;
        &lt;asp:CommandField ShowSelectButton="True" SelectText="Show" /&gt;

  protected void Button1_Click(object sender, EventArgs e)
     foreach (GridViewRow gvr in GridView1.Rows)
        if (((CheckBox)gvr.FindControl("CheckBoxItem")).Checked == true)
            int Index=gvr.RowIndex;
            string cid = ds.Tables[0].Rows[Index]["CID"].ToString();
            string delCM = string.Format("Delete Comments where CID={0}", cid);
            int r = dbm.DeleteFromDB(delCM);


Posted 9-May-13 8:54am
richcb at 9-May-13 14:03pm
How are you filling your gridview? SqlDataSource or dynamically?
tarannomvakili at 9-May-13 14:06pm
DBManager dbm = new DBManager();
DataSet ds = new DataSet();

public void LoadData()
string loadcm = string.Format("SELECT * FROM VCommentSend WHERE UID={0}", Session["UID"].ToString());
ds = dbm.GetTableData(loadcm);
GridView1.DataSource = ds;

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here is a simple way you can do this. Enable "Selecting" for your gridview and then template that field. When editing the template, you can replace the link button with a checkbox and use the CheckChanged event handler to delete the row selected from the database.

Here is what the deleting part could look like:
protected void CheckBox1_CheckChanged(object sender, EventArgs e)
    GridViewRow r = GridView1.SelectedRow;
    string delCM = string.Format("Delete Comments where CID={0}", r.Cells(0).ToString());
    int r = dbm.DeleteFromDB(delCM);
Where r.Cells(0).ToString() is going to be "CID" of the row you want to delete. If CID is the first column in the table leave the Cell index a "0". If not, change it to the proper index.
tarannomvakili at 9-May-13 14:34pm
Please tell you an example.
tarannomvakili at 9-May-13 14:46pm
I do not want ID displayed when the Gridview
richcb at 9-May-13 14:49pm
You will have to specify that when you fill your GridView then. As of now you are adding everything to it. Without the ID, you will have to find another way to identify the row and make it unique when you use it in your where clause.
tarannomvakili at 9-May-13 14:52pm
I want a function to get the index row from the check did not find rows in the dataset

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

  Print Answers RSS
0 OriginalGriff 475
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 290
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 550
1 Sergey Alexandrovich Kryukov 470
2 Maciej Los 310
3 ProgramFOX 275
4 Peter Leow 250

Advertise | Privacy | Mobile
Web04 | 2.8.150331.1 | Last Updated 9 May 2013
Copyright © CodeProject, 1999-2015
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