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

Delete item from arraylist and rebind a datagrid

P: n/a
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();
}

Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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();
}


Nov 16 '05 #2

P: n/a
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" wrote:
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();
}


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.