473,385 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Deleted rows reappear in databale and gridview

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
1 2127
Frinavale
9,735 Expert Mod 8TB
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

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

Similar topics

2
by: raffe | last post by:
Hi all. I'm struggeling with a problem. I trying to do something very simple. I have a DataSet containing two tables TableA and TableB. I use TableA.GetChildren("relationName") in order to get...
2
by: Snonck | last post by:
Is is possible to recover deleted rows from a table from archive logs? For example, say you have a database with archive logging turned off. You create a table and populate it with records. You...
4
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
I'm working in Visual Studio 2005 and C#. I have a DataTable with many rows. The user can delete a row. After the row is deleted, I'd like to column values for the deleted row, but any attempt to...
2
by: Vincent | last post by:
Hi, i want to know the number of rows in a gridview bound to a sqldatasource. I tried this: Protected Sub grv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
1
by: COHENMARVIN | last post by:
I have a gridview and I need to know the number of rows in it. I find the Page_load and the Gridview_Load events are too early to find out the number of rows. I can use the Gridview_databound...
0
by: Andy B | last post by:
Is there any simple examples of edit/deleteing rows in a GridView that has its datasource set to an xml object? I need to know, because the project I'm working on doesn't call for a database but...
2
by: =?Utf-8?B?QXN0cml0aA==?= | last post by:
I am a Newbie and i am scratching my head over how to delete a Row from a GridView. I have this GridView which has 3 fields called ID,ProjCode and ProjDescription of which ID is the hidden...
1
by: dbquest | last post by:
Hi, Is there a way to recover the deleted rows? We do have a backup but not for all the rows that got deleted. Appreciate some quick help on this. Thanks.
0
by: ZSvedic | last post by:
Hi all, I need to display DataRows with RowState.Deleted inside DataGridView, in a way that such rows have strikethrough font. Here is the screenshot how it should look like:...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.