G
Guest
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: -
<aspataGrid 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>
</aspataGrid>
Thanks for any help anyone can give me
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: -
<aspataGrid 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>
</aspataGrid>
Thanks for any help anyone can give me