473,385 Members | 1,492 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,385 software developers and data experts.

Deleting items in a datagrid

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 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>
Nov 16 '05 #1
0 1336

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
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...
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...
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....
9
by: Hamed | last post by:
Hello I have a DataGrid that a is bound to a DataTable. Some of the rows in the DataTable should not be deleted. How can I prohibit deleting of some identified rows? The problem could be...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.