Adding / Deleting between two DataTables / ListBoxes.

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

    Adding / Deleting between two DataTables / ListBoxes.

    Hi everyone,

    I have 2 listboxes that I need to move items between, they are both
    bound to DataTables which get populated from the database with a list
    of clients.

    Im getting a few problems, with remove I am getting
    InvalidArgument =Value of '0' is not valid for 'SelectedIndex' .
    Parameter name: SelectedIndex

    and sometimes when I try and add items that were previously not there
    I get
    "Deleted row information cannot be accessed through the row"


    private void btnAdd_Click(ob ject sender, EventArgs e)
    {
    //Add Clicked
    List<DataRowsel ectedrows = new List<DataRow>() ;
    foreach (DataRowView row in
    listAvailableCl ients.SelectedI tems)
    {

    selectedClients .ImportRow(row. Row);
    selectedrows.Ad d(row.Row);

    }
    foreach (DataRow row in selectedrows)
    {
    row.Delete();
    }
    }


    private void button2_Click(o bject sender, EventArgs e)
    //If remove clicked
    {
    List<DataRowsel ectedrows = new List<DataRow>() ;
    foreach (DataRowView row in
    listSelectedCli ents.SelectedIt ems)
    {
    availableClient s.ImportRow(row .Row);
    selectedrows.Ad d(row.Row);

    }
    foreach (DataRow row in selectedrows)
    {
    row.Delete();
    }
    }

    I know this has something to do with the CurrentRow properties of the
    DataView but I can't really get my head around it ;p

    Thanks heaps for any help,
    Laura, NZ

  • VJ

    #2
    Re: Adding / Deleting between two DataTables / ListBoxes.

    Ahh interesting Generics implementation. . I think your problem may be as the
    DataSet is "By Ref" object, so a copy is not created. Hope that gives you a
    hint?.. If you need more details, I will give it a detail look tomorrow.

    VJ

    "laurasaur" <laurasaur@gmai l.comwrote in message
    news:1173749109 .500832.254150@ 8g2000cwh.googl egroups.com...
    Hi everyone,
    >
    I have 2 listboxes that I need to move items between, they are both
    bound to DataTables which get populated from the database with a list
    of clients.
    >
    Im getting a few problems, with remove I am getting
    InvalidArgument =Value of '0' is not valid for 'SelectedIndex' .
    Parameter name: SelectedIndex
    >
    and sometimes when I try and add items that were previously not there
    I get
    "Deleted row information cannot be accessed through the row"
    >
    >
    private void btnAdd_Click(ob ject sender, EventArgs e)
    {
    //Add Clicked
    List<DataRowsel ectedrows = new List<DataRow>() ;
    foreach (DataRowView row in
    listAvailableCl ients.SelectedI tems)
    {
    >
    selectedClients .ImportRow(row. Row);
    selectedrows.Ad d(row.Row);
    >
    }
    foreach (DataRow row in selectedrows)
    {
    row.Delete();
    }
    }
    >
    >
    private void button2_Click(o bject sender, EventArgs e)
    //If remove clicked
    {
    List<DataRowsel ectedrows = new List<DataRow>() ;
    foreach (DataRowView row in
    listSelectedCli ents.SelectedIt ems)
    {
    availableClient s.ImportRow(row .Row);
    selectedrows.Ad d(row.Row);
    >
    }
    foreach (DataRow row in selectedrows)
    {
    row.Delete();
    }
    }
    >
    I know this has something to do with the CurrentRow properties of the
    DataView but I can't really get my head around it ;p
    >
    Thanks heaps for any help,
    Laura, NZ
    >

    Comment

    • Bruce Wood

      #3
      Re: Adding / Deleting between two DataTables / ListBoxes.

      On Mar 12, 6:25 pm, "laurasaur" <lauras...@gmai l.comwrote:
      Hi everyone,
      >
      I have 2 listboxes that I need to move items between, they are both
      bound to DataTables which get populated from the database with a list
      of clients.
      >
      Im getting a few problems, with remove I am getting
      InvalidArgument =Value of '0' is not valid for 'SelectedIndex' .
      Parameter name: SelectedIndex
      >
      and sometimes when I try and add items that were previously not there
      I get
      "Deleted row information cannot be accessed through the row"
      >
      private void btnAdd_Click(ob ject sender, EventArgs e)
      {
      //Add Clicked
      List<DataRowsel ectedrows = new List<DataRow>() ;
      foreach (DataRowView row in
      listAvailableCl ients.SelectedI tems)
      {
      >
      selectedClients .ImportRow(row. Row);
      selectedrows.Ad d(row.Row);
      >
      }
      foreach (DataRow row in selectedrows)
      {
      row.Delete();
      }
      }
      >
      private void button2_Click(o bject sender, EventArgs e)
      //If remove clicked
      {
      List<DataRowsel ectedrows = new List<DataRow>() ;
      foreach (DataRowView row in
      listSelectedCli ents.SelectedIt ems)
      {
      availableClient s.ImportRow(row .Row);
      selectedrows.Ad d(row.Row);
      >
      }
      foreach (DataRow row in selectedrows)
      {
      row.Delete();
      }
      }
      >
      I know this has something to do with the CurrentRow properties of the
      DataView but I can't really get my head around it ;p
      Well, I'm no expert on the precise inner workins of DataTables, but
      the first thing I would try is to modify the tables outside the
      selected items list, like this:

      private void btnAdd_Click(ob ject sender, EventArgs e)
      {
      //Add Clicked
      List<DataRowsel ectedrows = new List<DataRow>() ;
      foreach (DataRowView row in
      listAvailableCl ients.SelectedI tems)
      {
      selectedrows.Ad d(row.Row);
      }
      foreach (DataRow row in selectedrows)
      {
      availableClient s.Rows.Remove(r ow);
      selectedClients .ImportRow(row) ;
      }
      }

      The second thing is your use of the Delete method on the data row. I
      could be wrong, but I believe that all that does is flags the row as
      "deleted". It doesn't actually remove it from the collection of rows
      against the availableClient s.

      However, I think that the main thing here is to avoid messing with the
      state of any of the rows while you're iterating through the selected
      items in the list view. Gather your list of rows first, then act upon
      them.

      Comment

      • Bruce Wood

        #4
        Re: Adding / Deleting between two DataTables / ListBoxes.

        On Mar 12, 6:25 pm, "laurasaur" <lauras...@gmai l.comwrote:
        Hi everyone,
        >
        I have 2 listboxes that I need to move items between, they are both
        bound to DataTables which get populated from the database with a list
        of clients.
        >
        Im getting a few problems, with remove I am getting
        InvalidArgument =Value of '0' is not valid for 'SelectedIndex' .
        Parameter name: SelectedIndex
        >
        and sometimes when I try and add items that were previously not there
        I get
        "Deleted row information cannot be accessed through the row"
        >
        private void btnAdd_Click(ob ject sender, EventArgs e)
        {
        //Add Clicked
        List<DataRowsel ectedrows = new List<DataRow>() ;
        foreach (DataRowView row in
        listAvailableCl ients.SelectedI tems)
        {
        >
        selectedClients .ImportRow(row. Row);
        selectedrows.Ad d(row.Row);
        >
        }
        foreach (DataRow row in selectedrows)
        {
        row.Delete();
        }
        }
        >
        private void button2_Click(o bject sender, EventArgs e)
        //If remove clicked
        {
        List<DataRowsel ectedrows = new List<DataRow>() ;
        foreach (DataRowView row in
        listSelectedCli ents.SelectedIt ems)
        {
        availableClient s.ImportRow(row .Row);
        selectedrows.Ad d(row.Row);
        >
        }
        foreach (DataRow row in selectedrows)
        {
        row.Delete();
        }
        }
        >
        I know this has something to do with the CurrentRow properties of the
        DataView but I can't really get my head around it ;p
        Well, I'm no expert on the precise inner workins of DataTables, but
        the first thing I would try is to modify the tables outside the
        selected items list, like this:

        private void btnAdd_Click(ob ject sender, EventArgs e)
        {
        //Add Clicked
        List<DataRowsel ectedrows = new List<DataRow>() ;
        foreach (DataRowView row in
        listAvailableCl ients.SelectedI tems)
        {
        selectedrows.Ad d(row.Row);
        }
        foreach (DataRow row in selectedrows)
        {
        availableClient s.Rows.Remove(r ow);
        selectedClients .ImportRow(row) ;
        }
        }

        The second thing is your use of the Delete method on the data row. I
        could be wrong, but I believe that all that does is flags the row as
        "deleted". It doesn't actually remove it from the collection of rows
        against the availableClient s.

        However, I think that the main thing here is to avoid messing with the
        state of any of the rows while you're iterating through the selected
        items in the list view. Gather your list of rows first, then act upon
        them.

        Comment

        Working...