Delete items from an Arraylist and Datagrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen

    #1

    Delete items from an Arraylist and Datagrid

    I have got an event below to remove items from an arraylist and then to
    rebind the arraylist to the datagrid subsequently deleting the appropriate
    row. My problem is that my code makes sense and I think my logic seems fine
    but when I click the button on my datagrid nothing seems to happen. Have you
    any idea where Im going wrong. Was thinking it might have something to do
    with my page load/ postback but not really sure. Can someone please help me.

    My event handler looks like the following: -

    private void DataGrid1_Delet eCommand(object source,
    System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {
    //get a reference to the ArrayList
    ArrayList addresses;
    addresses = (ArrayList) ViewState["Addresses"];

    //get the index of the item to be deleted
    //(available in the EventArgs)
    int deleteIndex = e.Item.ItemInde x;

    //remove from ArrayList
    addresses.Remov eAt(deleteIndex );

    //save back to viewstate!
    ViewState("Addr esses") = addresses;

    //now, rebind
    DataGrid1.DataS ource = addresses;
    DataGrid1.DataB ind();
    }

    My datagrid code is as follows: -
    <asp:datagrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 26px; POSITION:
    absolute; TOP: 276px" tabIndex="7" runat="server" Width="689px"
    AutoGenerateCol umns="false" Height="81px">
    <Columns>
    <asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
    <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
    BackColor="Blac k"></HeaderStyle>
    <ItemStyle HorizontalAlign ="Left"></ItemStyle>
    </asp:BoundColumn >
    <asp:ButtonColu mn Text="Remove" CommandName="Re move">
    <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
    BackColor="Blac k"></HeaderStyle>
    <ItemStyle HorizontalAlign ="Center"></ItemStyle>
    </asp:ButtonColum n>
    </Columns>
    </asp:datagrid>

    My page load event is as follows:
    private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
    DataGrid1.DataB ind();
    }
    }
    }
    Thanks for any help anyone can give me
  • Kannan.V

    #2
    RE: Delete items from an Arraylist and Datagrid

    hi stephen.....

    i guess i replied to a similar post from you....

    below is the entire block of code which i have tested and is working
    perfectly fine
    when i click on the delete link in the grid, the item gets removed from the
    grid.

    ***********CODE *************** *************** ***********
    namespace TestWebApp
    {
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.P age
    {
    protected System.Web.UI.W ebControls.Data Grid DataGrid1;
    protected System.Web.UI.W ebControls.Data Grid DataGrid2;
    protected System.Web.UI.H tmlControls.Htm lForm Form1;
    private ArrayList addresses;

    private void Page_Load(objec t sender, System.EventArg s e)
    {
    // when the page is first loaded only
    if( !IsPostBack )
    {
    addresses = new ArrayList(5);
    addresses.Add(" 1");
    addresses.Add(" 2");
    addresses.Add(" 3");
    addresses.Add(" 4");
    addresses.Add(" 5");
    ViewState["Addresses"] = addresses;
    DataGrid2.DataS ource = addresses;
    DataGrid2.DataB ind();
    }
    // on subsequent PostBacks:
    else
    {
    addresses = (ArrayList) ViewState["Addresses"];
    if( addresses != null )
    {
    DataGrid2.DataS ource = addresses;
    DataGrid2.DataB ind();
    }
    }

    private void DataGrid2_Delet eCommand(object source,
    System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    {
    Response.Write( e.Item.ItemInde x);
    addresses.Remov eAt(e.Item.Item Index);
    DataGrid2.DataS ource = addresses;
    DataGrid2.DataB ind();
    }
    }
    }
    ************COD E Ends*********** *************** ********

    hope this works for you,
    In case this does not work send me a message.....
    Regds


    "Stephen" wrote:
    [color=blue]
    > I have got an event below to remove items from an arraylist and then to
    > rebind the arraylist to the datagrid subsequently deleting the appropriate
    > row. My problem is that my code makes sense and I think my logic seems fine
    > but when I click the button on my datagrid nothing seems to happen. Have you
    > any idea where Im going wrong. Was thinking it might have something to do
    > with my page load/ postback but not really sure. Can someone please help me.
    >
    > My event handler looks like the following: -
    >
    > private void DataGrid1_Delet eCommand(object source,
    > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
    > {
    > //get a reference to the ArrayList
    > ArrayList addresses;
    > addresses = (ArrayList) ViewState["Addresses"];
    >
    > //get the index of the item to be deleted
    > //(available in the EventArgs)
    > int deleteIndex = e.Item.ItemInde x;
    >
    > //remove from ArrayList
    > addresses.Remov eAt(deleteIndex );
    >
    > //save back to viewstate!
    > ViewState("Addr esses") = addresses;
    >
    > //now, rebind
    > DataGrid1.DataS ource = addresses;
    > DataGrid1.DataB ind();
    > }
    >
    > My datagrid code is as follows: -
    > <asp:datagrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 26px; POSITION:
    > absolute; TOP: 276px" tabIndex="7" runat="server" Width="689px"
    > AutoGenerateCol umns="false" Height="81px">
    > <Columns>
    > <asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
    > <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
    > BackColor="Blac k"></HeaderStyle>
    > <ItemStyle HorizontalAlign ="Left"></ItemStyle>
    > </asp:BoundColumn >
    > <asp:ButtonColu mn Text="Remove" CommandName="Re move">
    > <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
    > BackColor="Blac k"></HeaderStyle>
    > <ItemStyle HorizontalAlign ="Center"></ItemStyle>
    > </asp:ButtonColum n>
    > </Columns>
    > </asp:datagrid>
    >
    > My page load event is as follows:
    > private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
    > DataGrid1.DataB ind();
    > }
    > }
    > }
    > Thanks for any help anyone can give me[/color]

    Comment

    • Kannan.V

      #3
      RE: Delete items from an Arraylist and Datagrid

      hi...

      to add to my previous post....
      the following code also works fine with the arraylist.Remov e(object) method
      ************COD E************** *************** *********
      private void DataGrid2_Delet eCommand(object source,
      System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
      {
      Response.Write( e.Item.Cells[0].Text);
      addresses.Remov e(e.Item.Cells[0].Text);
      DataGrid2.DataS ource = addresses;
      DataGrid2.DataB ind();
      }
      *************** **Code End************ *************** *****

      In the above code i get the text value from the Cell[0] and remove that
      string from the arraylist.
      Both the methods are working fine.

      Regds,
      Kannan.V

      "Stephen" wrote:
      [color=blue]
      > I have got an event below to remove items from an arraylist and then to
      > rebind the arraylist to the datagrid subsequently deleting the appropriate
      > row. My problem is that my code makes sense and I think my logic seems fine
      > but when I click the button on my datagrid nothing seems to happen. Have you
      > any idea where Im going wrong. Was thinking it might have something to do
      > with my page load/ postback but not really sure. Can someone please help me.
      >
      > My event handler looks like the following: -
      >
      > private void DataGrid1_Delet eCommand(object source,
      > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
      > {
      > //get a reference to the ArrayList
      > ArrayList addresses;
      > addresses = (ArrayList) ViewState["Addresses"];
      >
      > //get the index of the item to be deleted
      > //(available in the EventArgs)
      > int deleteIndex = e.Item.ItemInde x;
      >
      > //remove from ArrayList
      > addresses.Remov eAt(deleteIndex );
      >
      > //save back to viewstate!
      > ViewState("Addr esses") = addresses;
      >
      > //now, rebind
      > DataGrid1.DataS ource = addresses;
      > DataGrid1.DataB ind();
      > }
      >
      > My datagrid code is as follows: -
      > <asp:datagrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 26px; POSITION:
      > absolute; TOP: 276px" tabIndex="7" runat="server" Width="689px"
      > AutoGenerateCol umns="false" Height="81px">
      > <Columns>
      > <asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
      > <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
      > BackColor="Blac k"></HeaderStyle>
      > <ItemStyle HorizontalAlign ="Left"></ItemStyle>
      > </asp:BoundColumn >
      > <asp:ButtonColu mn Text="Remove" CommandName="Re move">
      > <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
      > BackColor="Blac k"></HeaderStyle>
      > <ItemStyle HorizontalAlign ="Center"></ItemStyle>
      > </asp:ButtonColum n>
      > </Columns>
      > </asp:datagrid>
      >
      > My page load event is as follows:
      > private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
      > DataGrid1.DataB ind();
      > }
      > }
      > }
      > Thanks for any help anyone can give me[/color]

      Comment

      • Stephen

        #4
        RE: Delete items from an Arraylist and Datagrid

        Thanks your code works fine. I also got my code working by putting it in a
        different event handler. I put the code in the below ItemCommand event
        handler instead of the DeleteCommand hanndler and it worked fine for me. Not
        sure why, but got it working. I wonder do you have any idea why. Thanks very
        much again for your help.

        private void DataGrid1_ItemC ommand(object source,
        System.Web.UI.W ebControls.Data GridCommandEven tArgs e)

        "Kannan.V" wrote:
        [color=blue]
        > hi...
        >
        > to add to my previous post....
        > the following code also works fine with the arraylist.Remov e(object) method
        > ************COD E************** *************** *********
        > private void DataGrid2_Delet eCommand(object source,
        > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
        > {
        > Response.Write( e.Item.Cells[0].Text);
        > addresses.Remov e(e.Item.Cells[0].Text);
        > DataGrid2.DataS ource = addresses;
        > DataGrid2.DataB ind();
        > }
        > *************** **Code End************ *************** *****
        >
        > In the above code i get the text value from the Cell[0] and remove that
        > string from the arraylist.
        > Both the methods are working fine.
        >
        > Regds,
        > Kannan.V
        >
        > "Stephen" wrote:
        >[color=green]
        > > I have got an event below to remove items from an arraylist and then to
        > > rebind the arraylist to the datagrid subsequently deleting the appropriate
        > > row. My problem is that my code makes sense and I think my logic seems fine
        > > but when I click the button on my datagrid nothing seems to happen. Have you
        > > any idea where Im going wrong. Was thinking it might have something to do
        > > with my page load/ postback but not really sure. Can someone please help me.
        > >
        > > My event handler looks like the following: -
        > >
        > > private void DataGrid1_Delet eCommand(object source,
        > > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
        > > {
        > > //get a reference to the ArrayList
        > > ArrayList addresses;
        > > addresses = (ArrayList) ViewState["Addresses"];
        > >
        > > //get the index of the item to be deleted
        > > //(available in the EventArgs)
        > > int deleteIndex = e.Item.ItemInde x;
        > >
        > > //remove from ArrayList
        > > addresses.Remov eAt(deleteIndex );
        > >
        > > //save back to viewstate!
        > > ViewState("Addr esses") = addresses;
        > >
        > > //now, rebind
        > > DataGrid1.DataS ource = addresses;
        > > DataGrid1.DataB ind();
        > > }
        > >
        > > My datagrid code is as follows: -
        > > <asp:datagrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 26px; POSITION:
        > > absolute; TOP: 276px" tabIndex="7" runat="server" Width="689px"
        > > AutoGenerateCol umns="false" Height="81px">
        > > <Columns>
        > > <asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
        > > <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
        > > BackColor="Blac k"></HeaderStyle>
        > > <ItemStyle HorizontalAlign ="Left"></ItemStyle>
        > > </asp:BoundColumn >
        > > <asp:ButtonColu mn Text="Remove" CommandName="Re move">
        > > <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
        > > BackColor="Blac k"></HeaderStyle>
        > > <ItemStyle HorizontalAlign ="Center"></ItemStyle>
        > > </asp:ButtonColum n>
        > > </Columns>
        > > </asp:datagrid>
        > >
        > > My page load event is as follows:
        > > private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
        > > DataGrid1.DataB ind();
        > > }
        > > }
        > > }
        > > Thanks for any help anyone can give me[/color][/color]

        Comment

        • Kannan.V

          #5
          RE: Delete items from an Arraylist and Datagrid

          hi stephen....

          way cool...nice to hear that u got a working piece of code,
          writing it in either of the even handlers should work,
          the Item command event is invoked if anything is clicked inside the grid
          first and then the delete command handler is invoked if its present.
          The delete command handler can be thought of as a specific handler only for
          the delete button, similar thought holds good for the cancel and edit command
          handlers as opposed to the item command which fires for any click events in
          side the grid (delete, cancel, edit button clicks).

          Hope you might have got a small idea on this....

          Regds,
          Kannan.V



          "Stephen" wrote:
          [color=blue]
          > Thanks your code works fine. I also got my code working by putting it in a
          > different event handler. I put the code in the below ItemCommand event
          > handler instead of the DeleteCommand hanndler and it worked fine for me. Not
          > sure why, but got it working. I wonder do you have any idea why. Thanks very
          > much again for your help.
          >
          > private void DataGrid1_ItemC ommand(object source,
          > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
          >
          > "Kannan.V" wrote:
          >[color=green]
          > > hi...
          > >
          > > to add to my previous post....
          > > the following code also works fine with the arraylist.Remov e(object) method
          > > ************COD E************** *************** *********
          > > private void DataGrid2_Delet eCommand(object source,
          > > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
          > > {
          > > Response.Write( e.Item.Cells[0].Text);
          > > addresses.Remov e(e.Item.Cells[0].Text);
          > > DataGrid2.DataS ource = addresses;
          > > DataGrid2.DataB ind();
          > > }
          > > *************** **Code End************ *************** *****
          > >
          > > In the above code i get the text value from the Cell[0] and remove that
          > > string from the arraylist.
          > > Both the methods are working fine.
          > >
          > > Regds,
          > > Kannan.V
          > >
          > > "Stephen" wrote:
          > >[color=darkred]
          > > > I have got an event below to remove items from an arraylist and then to
          > > > rebind the arraylist to the datagrid subsequently deleting the appropriate
          > > > row. My problem is that my code makes sense and I think my logic seems fine
          > > > but when I click the button on my datagrid nothing seems to happen. Have you
          > > > any idea where Im going wrong. Was thinking it might have something to do
          > > > with my page load/ postback but not really sure. Can someone please help me.
          > > >
          > > > My event handler looks like the following: -
          > > >
          > > > private void DataGrid1_Delet eCommand(object source,
          > > > System.Web.UI.W ebControls.Data GridCommandEven tArgs e)
          > > > {
          > > > //get a reference to the ArrayList
          > > > ArrayList addresses;
          > > > addresses = (ArrayList) ViewState["Addresses"];
          > > >
          > > > //get the index of the item to be deleted
          > > > //(available in the EventArgs)
          > > > int deleteIndex = e.Item.ItemInde x;
          > > >
          > > > //remove from ArrayList
          > > > addresses.Remov eAt(deleteIndex );
          > > >
          > > > //save back to viewstate!
          > > > ViewState("Addr esses") = addresses;
          > > >
          > > > //now, rebind
          > > > DataGrid1.DataS ource = addresses;
          > > > DataGrid1.DataB ind();
          > > > }
          > > >
          > > > My datagrid code is as follows: -
          > > > <asp:datagrid id="DataGrid1" style="Z-INDEX: 108; LEFT: 26px; POSITION:
          > > > absolute; TOP: 276px" tabIndex="7" runat="server" Width="689px"
          > > > AutoGenerateCol umns="false" Height="81px">
          > > > <Columns>
          > > > <asp:BoundColum n DataField="Full Address" HeaderText="Ful l Address">
          > > > <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
          > > > BackColor="Blac k"></HeaderStyle>
          > > > <ItemStyle HorizontalAlign ="Left"></ItemStyle>
          > > > </asp:BoundColumn >
          > > > <asp:ButtonColu mn Text="Remove" CommandName="Re move">
          > > > <HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e"
          > > > BackColor="Blac k"></HeaderStyle>
          > > > <ItemStyle HorizontalAlign ="Center"></ItemStyle>
          > > > </asp:ButtonColum n>
          > > > </Columns>
          > > > </asp:datagrid>
          > > >
          > > > My page load event is as follows:
          > > > private void Page_Load(objec t sender, System.EventArg s 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.DataS ource = addresses;
          > > > DataGrid1.DataB ind();
          > > > }
          > > > }
          > > > }
          > > > Thanks for any help anyone can give me[/color][/color][/color]

          Comment

          Working...