469,651 Members | 1,672 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,651 developers. It's quick & easy.

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(object sender, EventArgs e)
{
//Add Clicked
List<DataRowselectedrows = 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<DataRowselectedrows = 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

Mar 13 '07 #1
3 2100
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

"laurasaur" <la*******@gmail.comwrote in message
news:11**********************@8g2000cwh.googlegrou ps.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(object sender, EventArgs e)
{
//Add Clicked
List<DataRowselectedrows = 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<DataRowselectedrows = 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

Mar 13 '07 #2
On Mar 12, 6:25 pm, "laurasaur" <lauras...@gmail.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(object sender, EventArgs e)
{
//Add Clicked
List<DataRowselectedrows = 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<DataRowselectedrows = 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<DataRowselectedrows = 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.

Mar 13 '07 #3
On Mar 12, 6:25 pm, "laurasaur" <lauras...@gmail.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(object sender, EventArgs e)
{
//Add Clicked
List<DataRowselectedrows = 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<DataRowselectedrows = 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<DataRowselectedrows = 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.

Mar 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Junkguy | last post: by
4 posts views Thread by bill yeager | last post: by
reply views Thread by Terry D | last post: by
1 post views Thread by J. A. Zanetti | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.