473,383 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Update one item in a BindingList

Please look at the code and the following question...

public BindingList<MyStruct myList ;

foreach ( Object item in myList )
{
MyStruct myStruct = (MyStruct)item;
myStruct .Name = "any new name" ;

///
/// Question: I need to update myList with the updated myStruct, how is the
most
/// efficient way to replace/update the old item in the list
}

Thanks
EitanB

Aug 24 '07 #1
1 14203
EitanB,

You should cycle through the items using the indexer, and not foreach,
like so:

for (int index = 0; index < myList.Count; ++index)
{
// Get the item.
MyStruct myStruct = myList[index];

// Modify the item here.

// Assign the item back.
myList[index] = myStruct;
}

The reassignment is necessary as you are working with what I assume is a
value type, so accessing properties off instances of that type returned by
the indexer would not change the value in the list.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Eitan" <Ei***@discussions.microsoft.comwrote in message
news:42**********************************@microsof t.com...
Please look at the code and the following question...

public BindingList<MyStruct myList ;

foreach ( Object item in myList )
{
MyStruct myStruct = (MyStruct)item;
myStruct .Name = "any new name" ;

///
/// Question: I need to update myList with the updated myStruct, how is
the
most
/// efficient way to replace/update the old item in the
list
}

Thanks
EitanB

Aug 24 '07 #2

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

Similar topics

3
by: guy | last post by:
i am new to generics so... if i have say dim MySecondList as new BindingList (Of MyFirstThing) dim MyFirstList as new BindingList (Of MySecondThing) how can i instantiate a new item withing...
4
by: hazz | last post by:
The data access layer below returns, well, a mess as you can see on the last line of this posting. What is the best way to return customer objects via a datareader from the data layer into my view...
1
by: Dave Booker | last post by:
Is there a reason why the BindingList constructor doesn't create a new BindingList? Create a form with four ListBoxes and the following will result in them all containing the exact same thing. ...
3
by: Bryan | last post by:
I am calling BindingList.EndEdit during a 'TextChanged' sub for a texbox. In theory, the key should be pressed changing the value in the text box, the EndEdit call should append the changes to the...
5
by: Mike Surcouf | last post by:
Hi All I have a stored procedure wrapper that returns Collection<T>. The wrapper is generated by codesmith so I don't really want to start altering it. I need to use this collection in a...
6
by: jwilson128 | last post by:
I am trying to decide whether to use a system.ComponentModel.BindingList(of T) or a Sytem.Collections.Generic.List(of T) for a custom collection. In testing a simple, read-only data binding to a...
1
by: Rick | last post by:
VS.net 2005 I'm using a bindinglist(of T) to bind to various controls on a WinForm. I use the name/value pairs in the "type" for the displayMember and ValueMember of say a ComboBox control. ...
1
by: =?Utf-8?B?RWl0YW4=?= | last post by:
Hello, I have a ComboBox named comboBoxSelChannel. I declared a structure named MySturct. public struct MyStruct { public int Index; public string Name;
1
by: jehugaleahsa | last post by:
Hello: I would like to perform an action on an item when it is removed from a BindingList<T>. However, I can only find out how to get the old index of the item, which doesn't really help me. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.