ASP.NET - gridviews problems - Asked By sunil pandey on 04-Mar-12 10:02 AM

hi all

i have a gridview with checkox. when  i am selecting any checkbox that row should be removed for always from gridview.
but it should be in database.
can any one help me..

[)ia6l0 iii replied to sunil pandey on 04-Mar-12 11:10 AM
I am not sure why would you "not" remove from database. That means the data would not be synchronized for the user with the database. Or perhaps, you are looking for a client-side script that removes and then later, you would sync it with server. 

Either the case be, this is what I would do:

Write a jQuery function to delete the row if the checkbox changes event.

Note, that you need to add the css "checked" to the checkbox. 
<asp:CheckBox ID="chkDelete" CssClass="checked" runat="server"/>

Or you can write it in a more generic fashion like this
$('table').delegate('input:checkbox', 'click', function () {

However to sync with the database, I would do this way.
Create a template field , add it to the columns collection. In the item template, add a checkbox. 
<asp:GridView ID="gridviewEmployees" runat="server" DataKeyNames="EmployeeID" >
            <asp:CheckBox ID="chkDelete" runat="server"/>

Set the DataKeyName to a column that would let you delete the rows. In the above example it is EmployeeID.
<asp:GridView ID="gridviewEmployees" runat="server" DataKeyNames="EmployeeID" >

And then anywhere in your code, loop thru the rows collection of the gridview and look for the checkbox control and its Checked property. 
        foreach (GridViewRow row in gridviewEmployees.Rows)
            CheckBox checkbox = (CheckBox)row.FindControl("chkDelete");
            if (checkbox.Checked)

int employeeID = Convert.ToInt32(gridviewEmployees.DataKeys[row.RowIndex].Value);
//Do whatever you want with the employeeID.

Hope this helps.
Web Star replied to sunil pandey on 04-Mar-12 11:56 AM
There are good example which show you how can delete row based on checkbox in gridview in
hope this helps you
dipa ahuja replied to sunil pandey on 04-Mar-12 12:01 PM

1. Take one column status in your table and set its value as 0

2. Bind your gridview with this query:


 string q = "select * from table1 where status=0";


3. Take checkbox inside the grid

     <asp:CheckBox ID="CheckBox1" runat="server" />


and a delete button

protected void CheckBox2_CheckedChanged(object sender, EventArgs e)


  CheckBox chk1 = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox2");

  foreach (GridViewRow r in GridView1.Rows)


    /* search id in itemtemplate */

    CheckBox chkin = (CheckBox)r.FindControl("CheckBox1");

    if (chkin.Checked)


      //getting datakey/primary key value

      string id = GridView1.DataKeys[r.RowIndex].Value.ToString();


      //fire update query

      string q = "Update table1 set status=1 where id=" + id;


      //rebind the griview here. call the method where you are binding gridview






kalpana aparnathi replied to sunil pandey on 04-Mar-12 02:26 PM

Try below code:

<asp:GridView ID="gridView1" runat="server" AutoGenerateColumns="False"
  DataSourceID="ObjectNewsletter" DataKeyNames="Id"/>

private List<string> GetSelectedItems()
  List<string> Selected = new List<string>();
  foreach (GridViewRow row in gridView1.Rows)
    CheckBox chx = (CheckBox)row.Cells[0].FindControl("checkBox1");
    if (chx == null || !chx.Checked) continue;
    if (gridView1.DataKeys != null)
      string id = gridView1.DataKeys[row.RowIndex].Value.ToString();
  return Selected;