i want to delete a row with the help of checkbox using gridview. But i don't want to delete from database.
as we do in e-shopping after selecting the product we want to delete a specific record from selecting list.
But it should not be deleted from database.
| for (int i = 0; i < GridView.Rows.Count; i++) { CheckBox cbb = (CheckBox)GridView.Rows[i].FindControl("CheckBox1"); if (cbb.Checked == true) { int id = Convert.ToInt32(GridView.Rows[i].Cells[1].Text); } |
it gives the id of that row.
but how it is deleted from the gridview
please help me
Kirtan PatelPosted Oct 10, 2009, 5:57 AM
Dont forget to mark "Do you like this Answer" please it will give me some credits :)
-------------------------------------------------------------------------------------------------
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
con.Open();
SqlCommand comm = new SqlCommand("select * from Users", con);
GridView1.DataSource = comm.ExecuteReader();
DataBind();
}
}
protected void btnHide_Click(object sender, EventArgs e)
{
foreach (GridViewRow r in GridView1.Rows)
{
if (((CheckBox)r.Cells[0].FindControl("CheckBox1")).Checked == true)
{
int index = r.RowIndex;
GridView1.Rows[index].Visible = false;
}
}
}
}
Roei BarPosted Oct 10, 2009, 10:09 AM
bind it to a session object that will conain all the data needed for the user to see
and also some type of an ItemId
on Bind Command bind the Checkbox to the ItemId
and when you click on the remove button you will get the Value of the Checked CheckBox and Remove it from the List
this will enable you not to delete from SQL and give you a a much better user performance, since you dont go to the DB for every command
and in the way that Kirtan stated, you will still have those items in the User Cart, meaning you will have to add a logic to ignore it.