Gavin Gaughran
Gavin Gaughran

Reputation: 55

Deleting row from Arraylist

Does anybody have any help,i am using an Arraylist to store details in a Session. When items are added to shopping cart i want to be able to have some functionality, i am beginning with a Delete button (eventually i would like an Edit button too) so i have tried two ways for the Delete button with neither working for me, first atttempt:

sc.aspx page

    <asp:TemplateField> 
    <ItemTemplate> 
        <asp:Button ID="btnDelete" runat="server" CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CommandName="deleterow" Text="Delete" /> 
    </ItemTemplate>
</asp:TemplateField>'

With:

'onrowcommand="DeleteRowBtn_Click"'

sc.aspx.cs

'protected void DeleteRowBtn_Click(object sender, GridViewCommandEventArgs e)
{
    int rowIndex = Convert.ToInt32(e.CommandArgument); 
}' 

second attempt:

sc.aspx

'OnRowDeleting="GridView1_RowDeleting"'

 '<asp:CommandField ShowDeleteButton="True" />'

sc.aspx.cs

'protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    ArrayList remove = (ArrayList)Session["aList"];
    DataRow dr = remove.Rows[e.RowIndex];
    remove.Rows.Remove(dr);
    GridView1.EditIndex = -1;
    FillShopCart();
}'

Upvotes: 0

Views: 1289

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460288

This doesn't work because an ArrayList has no Rows property but an indexer like arrays:

ArrayList remove = (ArrayList)Session["aList"];
DataRow dr = remove.Rows[e.RowIndex];

So this could work

DataRow dr = (DataRow)remove[e.RowIndex];

Sidenote: you should avoid ArrayList (or HashTable) if you're using at least .NET 2.0 and use the strong typed collections like List<T> or Dictionary<TKey, TValue> instead.

Upvotes: 1

Related Questions