473,883 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete items from an Arraylist and Datagrid

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 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_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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.ItemInde x;

//remove from ArrayList
addresses.Remov eAt(deleteIndex );

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

//now, rebind
DataGrid1.DataS ource = addresses;
DataGrid1.DataB ind();
}

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"
AutoGenerateCol umns="false" Height="81px">
<Columns>
<asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Left"></ItemStyle>
</asp:BoundColumn >
<asp:ButtonColu mn Text="Remove" CommandName="Re move">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
</asp:ButtonColum n>
</Columns>
</asp:datagrid>

My page load event is as follows:
private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
DataGrid1.DataB ind();
}
}
}
Thanks for any help anyone can give me
Jul 21 '05 #1
4 3912
hi stephen.....

i guess i replied to a similar post from you....

below is the entire block of code which i have tested and is working
perfectly fine
when i click on the delete link in the grid, the item gets removed from the
grid.

***********CODE *************** *************** ***********
namespace TestWebApp
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data Grid DataGrid1;
protected System.Web.UI.W ebControls.Data Grid DataGrid2;
protected System.Web.UI.H tmlControls.Htm lForm Form1;
private ArrayList addresses;

private void Page_Load(objec t sender, System.EventArg s e)
{
// when the page is first loaded only
if( !IsPostBack )
{
addresses = new ArrayList(5);
addresses.Add(" 1");
addresses.Add(" 2");
addresses.Add(" 3");
addresses.Add(" 4");
addresses.Add(" 5");
ViewState["Addresses"] = addresses;
DataGrid2.DataS ource = addresses;
DataGrid2.DataB ind();
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
DataGrid2.DataS ource = addresses;
DataGrid2.DataB ind();
}
}

private void DataGrid2_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
Response.Write( e.Item.ItemInde x);
addresses.Remov eAt(e.Item.Item Index);
DataGrid2.DataS ource = addresses;
DataGrid2.DataB ind();
}
}
}
************COD E Ends*********** *************** ********

hope this works for you,
In case this does not work send me a message.....
Regds
"Stephen" wrote:
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 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_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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.ItemInde x;

//remove from ArrayList
addresses.Remov eAt(deleteIndex );

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

//now, rebind
DataGrid1.DataS ource = addresses;
DataGrid1.DataB ind();
}

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"
AutoGenerateCol umns="false" Height="81px">
<Columns>
<asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Left"></ItemStyle>
</asp:BoundColumn >
<asp:ButtonColu mn Text="Remove" CommandName="Re move">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
</asp:ButtonColum n>
</Columns>
</asp:datagrid>

My page load event is as follows:
private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
DataGrid1.DataB ind();
}
}
}
Thanks for any help anyone can give me

Jul 21 '05 #2
hi...

to add to my previous post....
the following code also works fine with the arraylist.Remov e(object) method
************COD E************** *************** *********
private void DataGrid2_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
Response.Write( e.Item.Cells[0].Text);
addresses.Remov e(e.Item.Cells[0].Text);
DataGrid2.DataS ource = addresses;
DataGrid2.DataB ind();
}
*************** **Code End************ *************** *****

In the above code i get the text value from the Cell[0] and remove that
string from the arraylist.
Both the methods are working fine.

Regds,
Kannan.V

"Stephen" wrote:
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 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_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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.ItemInde x;

//remove from ArrayList
addresses.Remov eAt(deleteIndex );

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

//now, rebind
DataGrid1.DataS ource = addresses;
DataGrid1.DataB ind();
}

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"
AutoGenerateCol umns="false" Height="81px">
<Columns>
<asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Left"></ItemStyle>
</asp:BoundColumn >
<asp:ButtonColu mn Text="Remove" CommandName="Re move">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
</asp:ButtonColum n>
</Columns>
</asp:datagrid>

My page load event is as follows:
private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
DataGrid1.DataB ind();
}
}
}
Thanks for any help anyone can give me

Jul 21 '05 #3
Thanks your code works fine. I also got my code working by putting it in a
different event handler. I put the code in the below ItemCommand event
handler instead of the DeleteCommand hanndler and it worked fine for me. Not
sure why, but got it working. I wonder do you have any idea why. Thanks very
much again for your help.

private void DataGrid1_ItemC ommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

"Kannan.V" wrote:
hi...

to add to my previous post....
the following code also works fine with the arraylist.Remov e(object) method
************COD E************** *************** *********
private void DataGrid2_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
Response.Write( e.Item.Cells[0].Text);
addresses.Remov e(e.Item.Cells[0].Text);
DataGrid2.DataS ource = addresses;
DataGrid2.DataB ind();
}
*************** **Code End************ *************** *****

In the above code i get the text value from the Cell[0] and remove that
string from the arraylist.
Both the methods are working fine.

Regds,
Kannan.V

"Stephen" wrote:
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 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_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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.ItemInde x;

//remove from ArrayList
addresses.Remov eAt(deleteIndex );

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

//now, rebind
DataGrid1.DataS ource = addresses;
DataGrid1.DataB ind();
}

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"
AutoGenerateCol umns="false" Height="81px">
<Columns>
<asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Left"></ItemStyle>
</asp:BoundColumn >
<asp:ButtonColu mn Text="Remove" CommandName="Re move">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
</asp:ButtonColum n>
</Columns>
</asp:datagrid>

My page load event is as follows:
private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
DataGrid1.DataB ind();
}
}
}
Thanks for any help anyone can give me

Jul 21 '05 #4
hi stephen....

way cool...nice to hear that u got a working piece of code,
writing it in either of the even handlers should work,
the Item command event is invoked if anything is clicked inside the grid
first and then the delete command handler is invoked if its present.
The delete command handler can be thought of as a specific handler only for
the delete button, similar thought holds good for the cancel and edit command
handlers as opposed to the item command which fires for any click events in
side the grid (delete, cancel, edit button clicks).

Hope you might have got a small idea on this....

Regds,
Kannan.V

"Stephen" wrote:
Thanks your code works fine. I also got my code working by putting it in a
different event handler. I put the code in the below ItemCommand event
handler instead of the DeleteCommand hanndler and it worked fine for me. Not
sure why, but got it working. I wonder do you have any idea why. Thanks very
much again for your help.

private void DataGrid1_ItemC ommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

"Kannan.V" wrote:
hi...

to add to my previous post....
the following code also works fine with the arraylist.Remov e(object) method
************COD E************** *************** *********
private void DataGrid2_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
{
Response.Write( e.Item.Cells[0].Text);
addresses.Remov e(e.Item.Cells[0].Text);
DataGrid2.DataS ource = addresses;
DataGrid2.DataB ind();
}
*************** **Code End************ *************** *****

In the above code i get the text value from the Cell[0] and remove that
string from the arraylist.
Both the methods are working fine.

Regds,
Kannan.V

"Stephen" wrote:
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 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_Delet eCommand(object source,
System.Web.UI.W ebControls.Data GridCommandEven tArgs 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.ItemInde x;

//remove from ArrayList
addresses.Remov eAt(deleteIndex );

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

//now, rebind
DataGrid1.DataS ource = addresses;
DataGrid1.DataB ind();
}

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"
AutoGenerateCol umns="false" Height="81px">
<Columns>
<asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Left"></ItemStyle>
</asp:BoundColumn >
<asp:ButtonColu mn Text="Remove" CommandName="Re move">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
BackColor="Blac k"></HeaderStyle>
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
</asp:ButtonColum n>
</Columns>
</asp:datagrid>

My page load event is as follows:
private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
DataGrid1.DataB ind();
}
}
}
Thanks for any help anyone can give me

Jul 21 '05 #5

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

Similar topics

3
3060
by: Stephen | last post by:
I've got a datagrid with a remove button and I would like to add some code in the code behind page so as whenthe button is clicked the corresponding row in the datagrid is removed. The Datagrid is populated by the items in an arraylist shown in the code below. When the button is clicked I would also like the code to remove the items from the Arraylist. I've very little experience working with datagrids and arraylists so im finding this...
11
2188
by: Stephen | last post by:
I was wondering if someone can help me with an web application design problem. I have a aspx page which builds up an arraylist called addresses and outputs the values in the arraylist items to a datagrid. I am using the viewstate object to store the Arraylist items on the page on postback. My PROBLEM is that I need to redirect the user to a new aspx page and on this new page i need to be able to access the items in my arraylist. Is this...
0
1365
by: Stephen | last post by:
I have a datagrid with a remove button and I would like to add some code in the code behind page so as whenthe button is clicked the corresponding row in the datagrid is removed. The Datagrid is populated by the items in an arraylist shown in the code below. When the button is clicked I would also like the code to remove the items from the Arraylist. I've very little experience working with datagrids and arraylists so im finding this...
2
7816
by: Stephen | last post by:
I am trying to delete a row in a datagrid on the onclick of a asp:ButtonColumn. The datagrid is created from the items in an arraylist so what im trying to do is remove the item from the array and then rebind the datagrid. Below is the code I have to delete the items from the arraylist but nothing seems to happen when I run this code. Does any one have any idea where Im going wrong. Thanks for any help you can give me. private void...
1
9887
by: Stephen | last post by:
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...
4
334
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 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...
0
9953
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11166
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10868
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10422
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6009
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.