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 DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
string myAddress = e.Item.Cells[0].Text;
addresses.Remove("myAddress");
DataGrid1.DataSource = addresses;
DataGrid1.DataBind();
}
The code which I have to create the arraylist and populate the datagrid 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();
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
Address newAddress = new Address();
newAddress.Address1 = this.TextBox1.Text.Trim();
newAddress.Address2 = this.TextBox2.Text.Trim();
newAddress.Address3 = this.TextBox3.Text.Trim();
newAddress.Address4 = this.TextBox4.Text.Trim();
newAddress.Address5 = this.TextBox5.Text.Trim();
newAddress.Address6 = this.TextBox6.Text.Trim();
addresses.Add(newAddress);
ViewState["Addresses"] = addresses;
DataGrid1.DataSource = addresses;
DataGrid1.DataBind();
}