Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 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