-1

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??

Edited by Nick Evan: Fixed formatting

3
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by bhagyap
0

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.

Edited by Nick Evan: Fixed formatting

0

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

0

hi..

Thanks for ur reply..

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.