Solved

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

Posted on 2011-03-25
4
576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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