473,328 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,328 software developers and data experts.

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 3006
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Junkguy | last post by:
I'm having difficulty deleting rows from a datagrid. I want to put a "delete" button on a form and achieve the same functionality as hitting the "delete" key on the keyboard for the selected row of...
5
by: Mojtaba Faridzad | last post by:
Hi, with SetDataBinding( ) a DataGrid shows a DataView. user can select some rows in the grid by holding cotrol key. when user clicks on Delete button, I should delete all selected rows. I am...
0
by: Stephen | last post by:
I have 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...
5
by: nadeem_far | last post by:
Hello All, I have been trying to solve the following issue and it seems like there is no solution available any where and maybe people at Microsoft should may be of some help. I am writing a...
0
by: Hrvoje Vrbanc | last post by:
Hello, this is a problem I came upon while building a site based on MCMS 2002 but it's not strictly MCMS-oriented: I have a page that displays a certain content in presentation mode but when an...
4
by: Lisa | last post by:
Hey all, I've spent an hour reading posts to see how to delete a row in a datagrid but none of them work for me (most are VB.NET and I'm coding in C#). Specifically, I have a checkbox next to each...
0
by: steven.shannon | last post by:
Hello, I'm writing an app that involves deleting all the items in a specified Outlook folder regardless of item type (i.e. Contacts, Tasks, etc.). This code was ported from VBA where it worked...
3
by: Stephen | last post by:
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...
10
by: Nick | last post by:
Hello, Please pardon my ignorance as I'm sure this is easy to do. I have a datagrid where I want to let the user delete columns. I added a context menu to the datagrid that has a delete option....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.