By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,046 Members | 2,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,046 IT Pros & Developers. It's quick & easy.

Delete items from an Arraylist and Datagrid

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.