0

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();
}'
3
  • Please edit your question: 1. Add the binding code (FillShopCart?). 2. ArrayList don't have the Rows property. 3. Don't use ArrayList but List<T>. Commented Jul 11, 2012 at 14:07
  • 1
    Why ArrayList instead of List<T>? Are you using .net 1.x? Commented Jul 11, 2012 at 14:11
  • I was only taught Arraylist and never shown List<T>, this is my first ever 'project' using asp so i will have to try find some tutorials on List<T>.For this 'project' i need to use 2.0 My FillShopCart is as follows: private void FillShopCart() { ArrayList aList; aList = (ArrayList)Session["aList"]; GridView1.DataSource = aList; GridView1.DataBind(); } Commented Jul 11, 2012 at 20:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Tim, thank you for the response. but I still couldnt get this to work, i put in the line and even tried different ways and no joy, i now have ArrayList remove = (ArrayList)Session["aList"]; DataRow dr = (DataRow)remove[e.RowIndex]; remove.Remove(dr); Session["aList"] = remove; GridView1.EditIndex = -1; FillShopCart(); But i get "unable to cast object type of "CustomerOrder" to type of "System.Data.Datarow". CustomerOrder is my class where i retrieve the details for to be displayed
Then you need to cast it to CustomerOrder instead of DataRow. I don't know what this class has or can, but apparenetly it's the datasource of your GridView.
Thanks Tim, ended up with: ArrayList remove = (ArrayList)Session["aList"]; CustomerOrder order = (CustomerOrder)remove[e.RowIndex]; remove.Remove(order); Session["aList"] = remove; FillShopCart(); Thanks for your time...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.