473,387 Members | 1,492 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,387 software developers and data experts.

How do you delete data from a DataGrid?

I'm trying to delete data from a DataGrid using a ButtonColumn with a
CommandName="Delete" but it's not working a the min. I'm using session data
which fills a DataTable which then fills the DataGrid. The code is as
follows (dgBasket is the DataGrid):

private void Page_Load(object sender, System.EventArgs e)

{

DataTable myTable = (DataTable) Session["Basket"];

dgBasket.DataSource = myTable;

dgBasket.DataKeyField = "Product";

dgBasket.DataBind();

}
private void dgBasket_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

int rowToDelete = e.Item.ItemIndex;
DataTable myTable = (DataTable) Session["Basket"];

DataRowCollection myRows = myTable.Rows;

DataRow thisRow = myTable.Rows.Find(rowToDelete);

thisRow.Delete();

Session["Basket"] = myTable;

dgBasket.DataSource = myTable;

dgBasket.EditItemIndex = -1;

dgBasket.DataKeyField = "Product";

dgBasket.DataBind();

}

When I press one of the delete buttons nothing happens - the page reloads
but no info is deleted. Does anyone know where I'm going wrong?

Mark Jones
Nov 15 '05 #1
2 2549
Try not to databind the controls each time, only first time

if ( ! IsPostBack ) {
DataTable myTable = (DataTable) Session["Basket"];

dgBasket.DataSource = myTable;

dgBasket.DataKeyField = "Product";

dgBasket.DataBind();
}

in Page_Load

Hope this helps.

Dan Cimpoiesu

"Mark Jones" <ma************@btiniternet.com> wrote in message
news:bk**********@titan.btinternet.com...
I'm trying to delete data from a DataGrid using a ButtonColumn with a
CommandName="Delete" but it's not working a the min. I'm using session data which fills a DataTable which then fills the DataGrid. The code is as
follows (dgBasket is the DataGrid):

private void Page_Load(object sender, System.EventArgs e)

{

DataTable myTable = (DataTable) Session["Basket"];

dgBasket.DataSource = myTable;

dgBasket.DataKeyField = "Product";

dgBasket.DataBind();

}
private void dgBasket_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

int rowToDelete = e.Item.ItemIndex;
DataTable myTable = (DataTable) Session["Basket"];

DataRowCollection myRows = myTable.Rows;

DataRow thisRow = myTable.Rows.Find(rowToDelete);

thisRow.Delete();

Session["Basket"] = myTable;

dgBasket.DataSource = myTable;

dgBasket.EditItemIndex = -1;

dgBasket.DataKeyField = "Product";

dgBasket.DataBind();

}

When I press one of the delete buttons nothing happens - the page reloads
but no info is deleted. Does anyone know where I'm going wrong?

Mark Jones

Nov 15 '05 #2
Dan,

I added the code you suggested but to no avail. Any other ideas?

Mark Jones
"Dan Cimpoiesu" <da**********@gmx.net> wrote in message
news:ug**************@TK2MSFTNGP09.phx.gbl...
Try not to databind the controls each time, only first time

if ( ! IsPostBack ) {
DataTable myTable = (DataTable) Session["Basket"];

dgBasket.DataSource = myTable;

dgBasket.DataKeyField = "Product";

dgBasket.DataBind();
}

in Page_Load

Hope this helps.

Dan Cimpoiesu

"Mark Jones" <ma************@btiniternet.com> wrote in message
news:bk**********@titan.btinternet.com...
I'm trying to delete data from a DataGrid using a ButtonColumn with a
CommandName="Delete" but it's not working a the min. I'm using session

data
which fills a DataTable which then fills the DataGrid. The code is as
follows (dgBasket is the DataGrid):

private void Page_Load(object sender, System.EventArgs e)

{

DataTable myTable = (DataTable) Session["Basket"];

dgBasket.DataSource = myTable;

dgBasket.DataKeyField = "Product";

dgBasket.DataBind();

}
private void dgBasket_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

{

int rowToDelete = e.Item.ItemIndex;
DataTable myTable = (DataTable) Session["Basket"];

DataRowCollection myRows = myTable.Rows;

DataRow thisRow = myTable.Rows.Find(rowToDelete);

thisRow.Delete();

Session["Basket"] = myTable;

dgBasket.DataSource = myTable;

dgBasket.EditItemIndex = -1;

dgBasket.DataKeyField = "Product";

dgBasket.DataBind();

}

When I press one of the delete buttons nothing happens - the page reloads but no info is deleted. Does anyone know where I'm going wrong?

Mark Jones


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rameshika | last post by:
Hi All How can I select a particular row in a datagrid.Where when the user double clicks the data in that row should appear in the corresponding text boxes in runtime And how can the user...
3
by: Ryan Liu | last post by:
Hi there, I got a NullReferenceException when delete last row in a datagrid. I had hard time to solve since it does not occur in my own code. I put a datagrid in my inherited user control,...
1
by: J | last post by:
Hi, Can someone please help with a problem I have. The BOL I think are useless in explaining what I want to do. I have a datagrid, that is bound to a dataview. I have a checkbox as a...
3
by: vcornjamb | last post by:
Hello, I am developing a web form that contains some buttons and a data grid which has as its last column link buttons that will delete the data associated with that row. Everything works fine,...
0
by: Suzanne | last post by:
I'd like to know how can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key? I have found this code on...
5
by: Agnes | last post by:
I an addnew and modify all the rows in my Datagrid . However , If i highlight the whole rows , press 'delete' and the process daInv.update(dsInv,"mytable) I got the following error "This row has...
5
by: Jeff User | last post by:
Hello ..NET 1.1, VS 2003, C# & asp.net I have tried to follow msdn instructions and samples but I can not get an event to fire for this button on the datagrid. There has to be something obvious...
2
by: tomcarr1 | last post by:
In this walkthrough on "Allowing Users to Delete Items in a DataGrid Web Server Control" at: ...
5
by: rn5a | last post by:
The .NET 2.0 documentation states the following: When using a DataSet or DataTable in conjunction with a DataAdapter & a relational data source, use the Delete method of the DataRow to remove...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.