473,385 Members | 1,535 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,385 software developers and data experts.

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_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();
}
}
}
Thanks for any help anyone can give me
Jul 21 '05 #1
4 3846
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.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.DataGrid DataGrid2;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
private ArrayList addresses;

private void Page_Load(object sender, System.EventArgs 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.DataSource = addresses;
DataGrid2.DataBind();
}
// on subsequent PostBacks:
else
{
addresses = (ArrayList) ViewState["Addresses"];
if( addresses != null )
{
DataGrid2.DataSource = addresses;
DataGrid2.DataBind();
}
}

private void DataGrid2_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(e.Item.ItemIndex);
addresses.RemoveAt(e.Item.ItemIndex);
DataGrid2.DataSource = addresses;
DataGrid2.DataBind();
}
}
}
************CODE 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_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();
}
}
}
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.Remove(object) method
************CODE********************************** ****
private void DataGrid2_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(e.Item.Cells[0].Text);
addresses.Remove(e.Item.Cells[0].Text);
DataGrid2.DataSource = addresses;
DataGrid2.DataBind();
}
*****************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_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();
}
}
}
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_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

"Kannan.V" wrote:
hi...

to add to my previous post....
the following code also works fine with the arraylist.Remove(object) method
************CODE********************************** ****
private void DataGrid2_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(e.Item.Cells[0].Text);
addresses.Remove(e.Item.Cells[0].Text);
DataGrid2.DataSource = addresses;
DataGrid2.DataBind();
}
*****************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_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();
}
}
}
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_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)

"Kannan.V" wrote:
hi...

to add to my previous post....
the following code also works fine with the arraylist.Remove(object) method
************CODE********************************** ****
private void DataGrid2_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Response.Write(e.Item.Cells[0].Text);
addresses.Remove(e.Item.Cells[0].Text);
DataGrid2.DataSource = addresses;
DataGrid2.DataBind();
}
*****************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_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();
}
}
}
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
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...
11
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...
0
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...
2
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...
1
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...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.