Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populating a list itemsource with row from datatable

Posted on 2013-05-16
6
Medium Priority
?
396 Views
Last Modified: 2013-05-17
Hi guys,

So I basically want to populate a list.itemsource with the first row of a datatable (which is the headers, Firstname, Surname etc).

So far I have....

 List<string> lst = new List<string>();
                foreach (DataRow r in DtSet.Tables[0].Rows)
                {
                    lst.Add(r[0].ToString());
                    lst.Add(r[1].ToString());
                    lst.Add(r[2].ToString());
                    lst.Add(r[3].ToString());
                    lst.Add(r[4].ToString());
                }
                cmbImportedHeaders.Items.Clear();
                cmbImportedHeaders.ItemsSource = lst;

Open in new window


Where am I going wrong?

Thanks.
0
Comment
Question by:deanlee17
[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
  • 3
6 Comments
 
LVL 41

Accepted Solution

by:
Kyle Abrahams earned 2000 total points
ID: 39171771
http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms

Note you wouldn't need to loop over all the rows.

if (table.Rows.Count > 0)
   DataRow r = table.rows[0];
else
   // no rows, what happens?

then do your add for the one row.
0
 

Author Comment

by:deanlee17
ID: 39171779
Sorry, which post were you referring to? That top reply doesn't have a loop, or does it?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 39171784
foreach is a loop
0
 

Author Comment

by:deanlee17
ID: 39174100
Ok guys ive made a bit of a mistake, I do not want the first datarow, as I actually want the column names. I had...

 if (DtSet.Tables[0].Rows.Count > 0)
                {
                    DataRow r = DtSet.Tables[0].Rows[0];

                    cmbImportedHeaders.Items.Clear();
                    cmbImportedHeaders.ItemsSource = r.ItemArray;
                }

Open in new window


Which worked for the first row of data. But how can I change this for the column names. Baring in mind the number of columns will be varying as its from an excel import.

Thanks,
Dean/
0
 

Author Comment

by:deanlee17
ID: 39174248
Its ok ive done it..

  List<string> lst = new List<string>();

                foreach (DataColumn dc in (DtSet.Tables[0].Columns))
                {
                    lst.Add(dc.ToString());
                }

                cmbImportedHeaders.Items.Clear();
                cmbImportedHeaders.ItemsSource = lst; 

Open in new window

0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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