Solved

c# winform get all rows from one column into dictionary and bind to list?

Posted on 2011-03-25
4
568 Views
Last Modified: 2013-12-17
hi all my code below should hopefully explain where im trying to get to if someone could point out the errors and put me in the right direction?

i want all the rows from the column Trades in the Trades Table and get them in a dictionary and bind them

Thanks
Dictionary<string, string> dictATrades = new Dictionary<string, string>();
            foreach (DataRow trade in dataSetMisc.Trades.Rows)
            {
                dictATrades.Add((string)(trade["Trades"]), (string)(trade["Trades"]));
            }
            var dictATradesSort = (from entry in dictATrades orderby entry.Value ascending select entry);

            lstAvailbleTrades.DataSource = new BindingSource(dictATradesSort, null);
            lstAvailbleTrades.DisplayMember = "Value";
            lstAvailbleTrades.ValueMember = "key";

Open in new window

0
Comment
Question by:awilderbeast
4 Comments
 
LVL 4

Accepted Solution

by:
RGBDart earned 250 total points
ID: 35216485
I'm not sure why you need dictionary?
Use list instead.

Replace
this code

Dictionary<string, string> dictATrades = new Dictionary<string, string>();
            foreach (DataRow trade in dataSetMisc.Trades.Rows)
            {
                dictATrades.Add((string)(trade["Trades"]), (string)(trade["Trades"]));
            }
            var dictATradesSort = (from entry in dictATrades orderby entry.Value ascending select entry);

Open in new window


with this one

List<string> dictATrades = (from DataRow trade in dataSetMisc.Trades.Rows select (string)(trade["Trades"])).ToList();
 dictATrades.Sort();

Open in new window

0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 35217594
Hi awilderbeast;

I also do not know why you need a Dictionary in this case. I tested your code out and it seems to work fine. Are you receiving errors?

If you are open to another way of doing this using a Dictionary Linq does a nice job.

Dictionary<string, string> dictATrades = ( from t in dataSetMisc.Trades.AsEnumerable( )
                                           orderby t.Field<String>( "Trades" )
                                           select new
                                           {
                                               Key = t.Field<String>( "Trades" ),
                                               Value = t.Field<String>( "Trades" )
                                           } ).ToDictionary( K => K.Key, V => V.Value );

Open in new window


Fernando
0
 
LVL 2

Expert Comment

by:ericpeckham
ID: 35219794
For one thing, you're putting the same value in both the key and value properties of each dictionary element.  Could that be why it's not working for you?
0
 
LVL 1

Author Closing Comment

by:awilderbeast
ID: 35231168
thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now