By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Deleted rows reappear in databale and gridview

P: 2
Hi!!!
following is complete code for adding and deleting rows from grid view.
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Configuration;
  3. using System.Data;
  4. using System.Linq;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.HtmlControls;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Xml.Linq;
  12. using System.Windows.Forms;
  13.  
  14. public partial class _Default : System.Web.UI.Page 
  15. {
  16.     public static DataTable dt;
  17.     public static DataTable dt1;
  18.     protected void Page_Load(object sender, EventArgs e)
  19.     {
  20.         if (!Page.IsPostBack)
  21.         {
  22.             dt = new DataTable();
  23.  
  24.             DataColumn dc = new DataColumn("EName");
  25.  
  26.             dt.Columns.Add(dc);
  27.  
  28.             DataColumn dc1 = new DataColumn("EID");
  29.  
  30.             dt.Columns.Add(dc1);
  31.  
  32.         } 
  33.    }
  34.     protected void Button1_Click(object sender, EventArgs e)
  35.     {
  36.  
  37.         GridView1.DataSource = dt;
  38.         GridView1.DataBind();
  39.  
  40.  
  41.         if (ViewState["myTable"] != null)
  42.         {
  43.             dt = ViewState["myTable"] as DataTable;
  44.         }
  45.         ViewState["myTable"] = dt;
  46.         DataRow dr = dt.NewRow();
  47.         dr["EName"] = TextBox1.Text.ToString();
  48.         dr["EID"] = TextBox1.Text.ToString();
  49.         dt.Rows.Add(dr);
  50.         //dt.Rows.Remove(GridView1.DataKey.)
  51.        // GridViewDeleteEventHandler 
  52.         dt1 = (DataTable)ViewState["myTable"];
  53.         GridView1.DataSource = dt1;
  54.       //  GridView1.DeleteRow(e.RowIndex);
  55.         GridView1.DataBind();
  56.  
  57.     }
  58.  
  59.  
  60.  
  61.     protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
  62.     {
  63.  
  64.     }
  65.  
  66.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  67.     {
  68.         dt1.Rows.RemoveAt(e.RowIndex);
  69.         GridView1.DataSource = dt1;
  70.         GridView1.DataBind();
  71.     }
  72. }

But after deleting rows from grid view it reappears.
Kindly reply.
Mar 16 '12 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
You deleted the row from dt1 but then didn't store dt1 back into your ViewState...so the next time you retrieve dt1 from ViewState it will still contain the row.
Mar 20 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.