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

How do you delete data from a DataGrid?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.