Solved

Populating a list itemsource with row from datatable

Posted on 2013-05-16
6
389 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 40

Accepted Solution

by:
Kyle Abrahams earned 500 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

770 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