470,624 Members | 2,459 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

Deleting items from a Datagrid

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 populated by the items in an
arraylist shown in the code below. When the button is clicked I would also
like the code to remove the items from the Arraylist. I've very little
experience working with datagrids and arraylists so im finding this difficult
to do and would really appreciate it if I could get some help from anyone
with this.

I know how to get the event up but not sure what to add into the event below
to achieve what I need to do: -
private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

}

This is how I currently create the arraylist and populate the datagrid: -
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();
}

My datagrid code is as follows: -

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 73px; POSITION:
absolute; TOP: 306px" runat="server" Width="459px"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="Address1" ItemStyle-HorizontalAlign="Center"
HeaderText="Address1" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address2" ItemStyle-HorizontalAlign="Center"
HeaderText="Address2" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address3" ItemStyle-HorizontalAlign="Center"
HeaderText="Address3" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address4" ItemStyle-HorizontalAlign="Center"
HeaderText="Address4" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address5" ItemStyle-HorizontalAlign="Center"
HeaderText="Address5" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address6" ItemStyle-HorizontalAlign="Center"
HeaderText="Address6" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:ButtonColumn Text="Remove" ItemStyle-HorizontalAlign="Center"
HeaderText="" CommandName="Remove" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black"
HeaderStyle-ForeColor="White"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Thanks for any help anyone can give me
Jul 21 '05 #1
3 2843
hi stephen,

you have to delete the item form the arraylist.
arraylist object provides a remove method, u need to pass the item to be
removed, in your case you can pass the string to be deleted.

Ex.
addresses.Remove("myAddress");

your next question might be, how do i get the text from the currently
selected row.

the datagrid has a property called the itemindex which returns the currently
selected row index as an integer.

so to get the text from the first cell in the datagrid that is displaying
your data,
u might have to use something similar to the sample shown below.

e.Item.Cells[0].Text

this returns the text in the first cell in the row where the edit button was
clicked.
pass this text to the remove method of the arraylist.
After removing from the arraylist, rebind the datagrid.

Hope this was helpful to you.
Regds,
http://kannanv.blogspot.com
"Stephen" wrote:
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 populated by the items in an
arraylist shown in the code below. When the button is clicked I would also
like the code to remove the items from the Arraylist. I've very little
experience working with datagrids and arraylists so im finding this difficult
to do and would really appreciate it if I could get some help from anyone
with this.

I know how to get the event up but not sure what to add into the event below
to achieve what I need to do: -
private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

}

This is how I currently create the arraylist and populate the datagrid: -
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();
}

My datagrid code is as follows: -

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 73px; POSITION:
absolute; TOP: 306px" runat="server" Width="459px"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="Address1" ItemStyle-HorizontalAlign="Center"
HeaderText="Address1" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address2" ItemStyle-HorizontalAlign="Center"
HeaderText="Address2" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address3" ItemStyle-HorizontalAlign="Center"
HeaderText="Address3" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address4" ItemStyle-HorizontalAlign="Center"
HeaderText="Address4" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address5" ItemStyle-HorizontalAlign="Center"
HeaderText="Address5" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address6" ItemStyle-HorizontalAlign="Center"
HeaderText="Address6" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:ButtonColumn Text="Remove" ItemStyle-HorizontalAlign="Center"
HeaderText="" CommandName="Remove" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black"
HeaderStyle-ForeColor="White"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Thanks for any help anyone can give me

Jul 21 '05 #2
I tried the following and nothing seemed to have happened. Am I along the
correct lines. Thanks for your help so far I definitely understand how to do
it now, im just having difficulty executing it with the code. Do you have
any idea where I am going wrong. At present when the button is clicked
nothing seems to be happening.

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

"Kannan.V" wrote:
hi stephen,

you have to delete the item form the arraylist.
arraylist object provides a remove method, u need to pass the item to be
removed, in your case you can pass the string to be deleted.

Ex.
addresses.Remove("myAddress");

your next question might be, how do i get the text from the currently
selected row.

the datagrid has a property called the itemindex which returns the currently
selected row index as an integer.

so to get the text from the first cell in the datagrid that is displaying
your data,
u might have to use something similar to the sample shown below.

e.Item.Cells[0].Text

this returns the text in the first cell in the row where the edit button was
clicked.
pass this text to the remove method of the arraylist.
After removing from the arraylist, rebind the datagrid.

Hope this was helpful to you.
Regds,
http://kannanv.blogspot.com
"Stephen" wrote:
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 populated by the items in an
arraylist shown in the code below. When the button is clicked I would also
like the code to remove the items from the Arraylist. I've very little
experience working with datagrids and arraylists so im finding this difficult
to do and would really appreciate it if I could get some help from anyone
with this.

I know how to get the event up but not sure what to add into the event below
to achieve what I need to do: -
private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

}

This is how I currently create the arraylist and populate the datagrid: -
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();
}

My datagrid code is as follows: -

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 73px; POSITION:
absolute; TOP: 306px" runat="server" Width="459px"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="Address1" ItemStyle-HorizontalAlign="Center"
HeaderText="Address1" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address2" ItemStyle-HorizontalAlign="Center"
HeaderText="Address2" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address3" ItemStyle-HorizontalAlign="Center"
HeaderText="Address3" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address4" ItemStyle-HorizontalAlign="Center"
HeaderText="Address4" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address5" ItemStyle-HorizontalAlign="Center"
HeaderText="Address5" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address6" ItemStyle-HorizontalAlign="Center"
HeaderText="Address6" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:ButtonColumn Text="Remove" ItemStyle-HorizontalAlign="Center"
HeaderText="" CommandName="Remove" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black"
HeaderStyle-ForeColor="White"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Thanks for any help anyone can give me

Jul 21 '05 #3
hi stephen......

from the code you have shown below,
****
string myAddress = e.Item.Cells[0].Text;
addresses.Remove("myAddress");
****

you have assigned the text from the datagrid to a string variable and in the
second line you are trying to remove a static string by enclosing it within
"Quotes".......
remove the "" form the myAddress.
myAddress is a variable of string type and it will replace the value inside
the remove method with the string returned from the previous line and the
item that matches that string will be remove from the arraylist....

Hope it worked....
Regds
http://kannanv.blogspot.com

"Stephen" wrote:
I tried the following and nothing seemed to have happened. Am I along the
correct lines. Thanks for your help so far I definitely understand how to do
it now, im just having difficulty executing it with the code. Do you have
any idea where I am going wrong. At present when the button is clicked
nothing seems to be happening.

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

"Kannan.V" wrote:
hi stephen,

you have to delete the item form the arraylist.
arraylist object provides a remove method, u need to pass the item to be
removed, in your case you can pass the string to be deleted.

Ex.
addresses.Remove("myAddress");

your next question might be, how do i get the text from the currently
selected row.

the datagrid has a property called the itemindex which returns the currently
selected row index as an integer.

so to get the text from the first cell in the datagrid that is displaying
your data,
u might have to use something similar to the sample shown below.

e.Item.Cells[0].Text

this returns the text in the first cell in the row where the edit button was
clicked.
pass this text to the remove method of the arraylist.
After removing from the arraylist, rebind the datagrid.

Hope this was helpful to you.
Regds,
http://kannanv.blogspot.com
"Stephen" wrote:
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 populated by the items in an
arraylist shown in the code below. When the button is clicked I would also
like the code to remove the items from the Arraylist. I've very little
experience working with datagrids and arraylists so im finding this difficult
to do and would really appreciate it if I could get some help from anyone
with this.

I know how to get the event up but not sure what to add into the event below
to achieve what I need to do: -
private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{

}

This is how I currently create the arraylist and populate the datagrid: -
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();
}

My datagrid code is as follows: -

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 73px; POSITION:
absolute; TOP: 306px" runat="server" Width="459px"
AutoGenerateColumns="false">
<Columns>
<asp:BoundColumn DataField="Address1" ItemStyle-HorizontalAlign="Center"
HeaderText="Address1" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address2" ItemStyle-HorizontalAlign="Center"
HeaderText="Address2" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address3" ItemStyle-HorizontalAlign="Center"
HeaderText="Address3" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address4" ItemStyle-HorizontalAlign="Center"
HeaderText="Address4" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address5" ItemStyle-HorizontalAlign="Center"
HeaderText="Address5" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:BoundColumn DataField="Address6" ItemStyle-HorizontalAlign="Center"
HeaderText="Address6" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black" HeaderStyle-ForeColor="White"></asp:BoundColumn>
<asp:ButtonColumn Text="Remove" ItemStyle-HorizontalAlign="Center"
HeaderText="" CommandName="Remove" HeaderStyle-HorizontalAlign="Center"
HeaderStyle-BackColor="black"
HeaderStyle-ForeColor="White"></asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Thanks for any help anyone can give me

Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Junkguy | last post: by
5 posts views Thread by Mojtaba Faridzad | last post: by
reply views Thread by Stephen | last post: by
5 posts views Thread by nadeem_far | last post: by
4 posts views Thread by Lisa | last post: by
3 posts views Thread by Stephen | last post: by
10 posts views Thread by Nick | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.