473,785 Members | 2,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Mar 13 '07 #1
3 2248
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*******@gmai l.comwrote in message
news:11******** **************@ 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

Mar 13 '07 #2
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.

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

Mar 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4183
by: Junkguy | last post by:
I'm having difficulty deleting rows from a datagrid. I want to put a "delete" button on a form and achieve the same functionality as hitting the "delete" key on the keyboard for the selected row of a datagrid. I really want to do it generically so I have subclassed datagrid and made my own delete row method but I can only get it to work for datasources that are datatables or dataviews. Specifically when the datasource is a DataSet I...
4
1916
by: bill yeager | last post by:
I have several template columns inside of a datagrid. Inside of these template columns are databound listboxes: <asp:TemplateColumn HeaderText="Crew Chiefs"> <ItemTemplate> <asp:listbox AutoPostBack="False" BackColor="#ffffff" id="lstCrewChief" runat="server" Rows="1" DataSource="<%# DsCrewChief1 %>" Enabled="True"
1
1585
by: psb | last post by:
I thought this was weird?? is this a bug in framework 1.0??? (1.0 is the version I am running against) --------------------------- dim dtAll as new datatable dim dtTmp as datatable dtTmp = LoadDataTable("select top 100 from table...") for i=0 to dtTmp.rows.count -1 ''''ERROR ON THIS LINE''' dtAll.Rows.Add(dtTmp.rows(i))
0
1101
by: Terry D | last post by:
I'm having an issue with an ASP.NET page (VS.NET 2003, VB.NET, Oracle back end). The page uses the standard VS.NET grid to display the records from a particular table. The user can edit certain fields in the grid. There are 10 columns in the grid, 5 static fields (labels), 4 listboxes, and 1 edit box. 2 of the listboxes hold about 12 items each and the other 2 listboxes only contain 2 items. The problem is that on some lower end...
1
2034
by: seanmayhew | last post by:
I have a form page that that while editing saves the data to an xml doc before submitting to db. On each page unload it saves the xmldoc as the user can add multiple items to the company like product types etc. So for instance Im adding a fruit company while adding a fruit company I allow the user to add types of fruit they carry and display it dynamically using an <asp:table> with image
1
1771
by: J. A. Zanetti | last post by:
Hello NG, i have this problem: in my application, i have a master- detail form displaying two DataGrids binded to DataViews. While the bindings work fine when moving through the grids' rows or making insertions, i keep encountering a problem when trying to delete the last row in a grid (problem occurs with both grids). Deleting any row other than the last will complete successfully. I have tried to handle all the underlying DataViews'...
8
1884
by: NAdir | last post by:
Hi, thank you for your help. My VB.Net application contains a document that the user can refresh at any time. The refresh works fine and needs to loop through few datatables (hundreds of rows). This works fine until I delete some rows in two tables. Just after the delete if I do the refresh there is a huge memory allocated and the time needed to perform the refresh increase, the memory and time continue to increase on each refresh until I...
1
1714
by: smartchap009 | last post by:
Adding,Updating and Deleting user using perl script in linux -------------------------------------------------------------------------------- hi, If i can get help in writing perl code for adding updating and deleting user in linux.adds a user to the database file called passwd. With the -a switch the program accepts a user-id and then prompts for a password, and a conformation. The username and password are then added to the passwd...
8
2016
by: jehugaleahsa | last post by:
Hello: We wrote an entire application where we add our DataRows to our DataTables immediately. However, we have to shut off our constraints to do this. We would like to use detached DataRows to circumvent this. What do you normally do to track detached DataRows? Is there a way to retrieve them? or do I have to stored them in some temporary location? Thanks for any input!
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9953
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8978
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.