Solved

Populating a list itemsource with row from datatable

Posted on 2013-05-16
6
388 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
  • 3
6 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
foreach is a loop
0
 

Author Comment

by:deanlee17
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now