Solved

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

Posted on 2011-03-25
4
573 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 63

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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