Hannes
Hannes

Reputation: 140

ASP.NET Get correct listview item so I can delete it

protected void myOrdersListView_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            if (e.CommandName == "removeBtn")
            {             
                CabinTable.Rows.RemoveAt(Convert.ToInt32(e.Item.DataItemIndex));
                CabinAdapter.Update(CabinTable);
                Response.Redirect("Overview.aspx");
            }
        }

I have a database in Windows Azure and I want to delete a row after I click on a linkbutton in a listview. I have tried 100 different ways but I cant get it to work, please help me out!

How can I get the correct parameter in here? "CabinTable.Rows.RemoveAt(?)"

Upvotes: 0

Views: 68

Answers (1)

ps2goat
ps2goat

Reputation: 8485

Instead of removing a row from the collection, delete the row itself:

CabinTable.Rows[Convert.ToInt32(e.Item.DataItemIndex)].Delete();

The sample also calls CabinTable.AcceptChanges(); after doing deletes, though I haven't tested to see if that is necessary in your case.

http://msdn.microsoft.com/en-us/library/system.data.datarow.delete(v=vs.110).aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2

Upvotes: 1

Related Questions