Adding / Deleting between two DataTables / ListBoxes.

L

laurasaur

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(object sender, EventArgs e)
{
//Add Clicked
List<DataRow> selectedrows = new List<DataRow>();
foreach (DataRowView row in
listAvailableClients.SelectedItems)
{

selectedClients.ImportRow(row.Row);
selectedrows.Add(row.Row);

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


private void button2_Click(object sender, EventArgs e)
//If remove clicked
{
List<DataRow> selectedrows = new List<DataRow>();
foreach (DataRowView row in
listSelectedClients.SelectedItems)
{
availableClients.ImportRow(row.Row);
selectedrows.Add(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
 
V

VJ

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
 
B

Bruce Wood

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(object sender, EventArgs e)
{
//Add Clicked
List<DataRow> selectedrows = new List<DataRow>();
foreach (DataRowView row in
listAvailableClients.SelectedItems)
{

selectedClients.ImportRow(row.Row);
selectedrows.Add(row.Row);

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

private void button2_Click(object sender, EventArgs e)
//If remove clicked
{
List<DataRow> selectedrows = new List<DataRow>();
foreach (DataRowView row in
listSelectedClients.SelectedItems)
{
availableClients.ImportRow(row.Row);
selectedrows.Add(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(object sender, EventArgs e)
{
//Add Clicked
List<DataRow> selectedrows = new List<DataRow>();
foreach (DataRowView row in
listAvailableClients.SelectedItems)
{
selectedrows.Add(row.Row);
}
foreach (DataRow row in selectedrows)
{
availableClients.Rows.Remove(row);
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 availableClients.

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.
 
B

Bruce Wood

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(object sender, EventArgs e)
{
//Add Clicked
List<DataRow> selectedrows = new List<DataRow>();
foreach (DataRowView row in
listAvailableClients.SelectedItems)
{

selectedClients.ImportRow(row.Row);
selectedrows.Add(row.Row);

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

private void button2_Click(object sender, EventArgs e)
//If remove clicked
{
List<DataRow> selectedrows = new List<DataRow>();
foreach (DataRowView row in
listSelectedClients.SelectedItems)
{
availableClients.ImportRow(row.Row);
selectedrows.Add(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(object sender, EventArgs e)
{
//Add Clicked
List<DataRow> selectedrows = new List<DataRow>();
foreach (DataRowView row in
listAvailableClients.SelectedItems)
{
selectedrows.Add(row.Row);
}
foreach (DataRow row in selectedrows)
{
availableClients.Rows.Remove(row);
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 availableClients.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top