Delete item from arraylist and rebind a datagrid

G

Guest

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();
}
 
Y

yameng

try this:
DataGrid1.DataSource = null;
DataGrid1.DataSource = addresses;
DataGrid1.DataBind();
 
G

Guest

Tried this and still got no joy. Im not sure if the e.Item.Cells[0].Text;
is correct as im trying to select the whole row as each row is an element of
the array. I dont know what im doing wrong at all to be honest. Im pretty
new to datagrids and arraylists so finding this task fairly difficult. have
you any more suggestions and do you know how I change the line string
myAddress = e.Item.Cells[0].Text; so as I pass in a string value of the whole
row selected. Thanks for your help so far.

ArrayList addresses;
addresses = (ArrayList) ViewState["Addresses"];
string myAddress = e.Item.Cells[0].Text;
addresses.Remove("myAddress");
DataGrid1.DataSource = null;
DataGrid1.DataSource = addresses;
DataGrid1.DataBind();

yameng said:
try this:
DataGrid1.DataSource = null;
DataGrid1.DataSource = addresses;
DataGrid1.DataBind();

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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top