473,403 Members | 2,183 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,403 software developers and data experts.

Delete items from an ArrayList

I have an event below to remove items from an arraylist and then to rebind
the arraylist to the datagrid subsequently deleting the appropriate row. My
problem is that my code makes sense and I think my logic seems fine but when
I click the button on my datagrid nothing seems to happen. Have you any idea
where Im going wrong. Was thinking it might have something to do with my page
load/ postback but not really sure. Can someone please help me.

My event handler looks like the following: -

private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
//get a reference to the ArrayList
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];

//get the index of the item to be deleted
//(available in the EventArgs)
int deleteIndex = e.Item.ItemIndex;

//remove from ArrayList
addresses.RemoveAt(deleteIndex);

//save back to viewstate!
ViewState("Addresses") = addresses;

//now, rebind
DataGrid1.DataSource = addresses;
DataGrid1.DataBind();
}

My datagrid code is as follows: -
<asp:datagrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 26px; POSITION:
absolute; TOP: 276px" tabIndex="7" runat="server" Width="689px"
AutoGenerateColumns="false" Height="81px">
<Columns>
<asp:BoundColumn DataField="FullAddress" HeaderText="Full Address">
<HeaderStyle HorizontalAlign="Center" ForeColor="White"
BackColor="Black"></HeaderStyle>
<ItemStyle HorizontalAlign="Left"></ItemStyle>
</asp:BoundColumn>
<asp:ButtonColumn Text="Remove" CommandName="Remove">
<HeaderStyle HorizontalAlign="Center" ForeColor="White"
BackColor="Black"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
</asp:ButtonColumn>
</Columns>
</asp:datagrid>

My page load event is as follows:
private void Page_Load(object sender, System.EventArgs e)
{
ArrayList addresses;

// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList(5);
ViewState["Addresses"] = addresses;
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
DataGrid1.DataSource = addresses;
DataGrid1.DataBind();
}
}
}

Nov 16 '05 #1
1 9868
Hi,

DataGrid1_DeleteCommand:

This method is ok, except this line that is not needed:
ViewState("Addresses") = addresses;

remember that you are dealing with references
The problem is in the Load event:

You are binding the grid EACHTIME you postback, you SHOULD NOT do that.
just remove the else of the if ( !IsPostBack ) and it should work fine.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 16 '05 #2

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

Similar topics

9
by: Birgit Rahm | last post by:
Hello newsgroup, I am a beginner, so I am asking maybe immoderate questions. I want to delete a variable, after filling it with complex objects. How should I do it? e.g. AAA = AAA = Can I...
1
by: Colin Steadman | last post by:
I have a multi-dimensional array that I want to delete items from. To do this I display a form and the user clicks some tick boxes. Then on the delete page I check which tick boxes were...
3
by: wja | last post by:
I'm still new to VB.NET. I'm having an issue sometimes when I place any control (we'll use a label for now) on a form then I right-click on the item, it won't let me delete it. I can do this...
4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
2
by: tomcarr1 | last post by:
In this walkthrough on "Allowing Users to Delete Items in a DataGrid Web Server Control" at: ...
8
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more...
0
by: =?Utf-8?B?U2hpcmxleQ==?= | last post by:
too large to be sent? I highlight, then press delete, and nothing happens! Frustrations frustration frustration!!!!!!!!! -- Shirley
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.