Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-03-25
4
Medium Priority
?
579 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 1000 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 64

Assisted Solution

by:Fernando Soto
Fernando Soto earned 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

927 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