Solved

Populating a list itemsource with row from datatable

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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