Hi..

I am populating my gridview as follows:-

 protected void Button1_Click(object sender, EventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlDataAdapter sda = new SqlDataAdapter("Select * from DropDownFilter", con);
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

and then i have checkbox for every row now i want to delete the checked rows on another button event,how can i achieve this??

Can anyone please help me??

following is the way to solve it: (please remember this is not the exact code)

 Delete Button click event()
 {
   foreach (Gridviewrow gvr in GridView1.Rows)
    {
      checkbox chkdelete = gvr.findcontrols("chkbox1") as checkbox; //the checkbox name in your gridview//

      if (chkdelete.checked)
      {
         //delete the row from db
         //it should be based on any primary key e.g. the gridview key

      }

    }

   //Bind the GridView1 again from DB since your gridview should be updated

  }

Please let us know if you need more. Sorry I don't beleive the exact code giving instant I want you to use your skills as well.

Hi,
You can do this by just write simple code.

1)Put add custom template field
2)In first or last coloum put checkbox.(Suppose ID is CheckBox1)

3)Write this code
Note: In this code I am assuming that you are working with asp.net using C#.And you can set case sensitive mistakes which may be occur in this code.you have already set an sql connection (con) and your dataset id is ds .Label1(Template Control) is bind with unique database table field Id.
for(int i=0;i<ds.rows.count;i++)
{
checkbox cb=((CheckBox)GridView1.Rows.FindControl(CheckBox1));
if(cb.Checked)
{
int id=((Label)GridView1.Rows.FindControl(Label1));
string s="delete from [tablename] where id="+id;
sqlcommand cmd=new sqlcommand(s,con);
con.open();
cmd.executeQry();
con.close();
fillgrid();//This is an method to fill grid view by using sql select command which you have write already to fill grid view.
}
}

Regards
Websoftcreation,jaipur

hi..

Thanks for ur reply..

But i am not understanding how r u fetching rows from an DataSet???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.