Click here to Skip to main content
11,928,569 members (53,690 online)
Rate this:
Please Sign up or sign in to vote.
See more: ASP.NET4
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"  >
         <asp:CheckBox  runat="server" ID="CheckBoxItem"   />
        <asp:BoundField DataField="Username" HeaderText="Username" />
        <asp:BoundField DataField="VideoTitle" HeaderText="VideoTitle" />
        <asp:BoundField DataField="LineDescription" HeaderText="Comment" />
        <asp:BoundField DataField="RegDate"  HeaderText="RegDate"/>
        <asp:BoundField DataField="RegTime" HeaderText="RegTime" />
        <asp:CommandField ShowSelectButton="True" SelectText="Show" />

  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 9-May-13 14:03pm
How are you filling your gridview? SqlDataSource or dynamically?
tarannomvakili 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 9-May-13 14:34pm
Please tell you an example.
tarannomvakili 9-May-13 14:46pm
I do not want ID displayed when the Gridview
richcb 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 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.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