Solved

Populating a list itemsource with row from datatable

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JSON  parse help 7 40
Adding items to a C# list incrementally 5 33
need to convert function to c# 5 31
C# Linq Select From List 3 17
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

828 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