Solved

Load data into datatable and set as itemsource

Posted on 2013-01-28
2
434 Views
Last Modified: 2013-01-28
Hi guys,

I am trying to load data into a datatable and set the datatable as the itemsource of a datagrid.

My code below seems until it comes to setting the itemsource of the datagrid to t, the error message is....

Cannot implicitly convert type 'system.data.datatable' tp type 'system.collections.IEnumerable' and explicit conversion exists.

 private void LoadDataset()
        {

            using (SqlConnection c = new SqlConnection("XXX"))
            {
                c.Open();

                using (SqlDataAdapter a = new SqlDataAdapter("select * from SQ_SysproHis_Step3", c))
                {
                    DataTable t = new DataTable();
                    a.Fill(t);

                    DataTableV2.ItemsSource = t;
                }
            }
            
        }

Open in new window


Many Thanks,
Dean
0
Comment
Question by:deanlee17
2 Comments
 
LVL 9

Expert Comment

by:sognoct
ID: 38826552
if DataTableV2 is your datagrid , you should use:

DataTableV2.datasource = t
0
 
LVL 6

Accepted Solution

by:
esolve earned 500 total points
ID: 38826562
//ASP.NET
DataGrid1.DataSource = ds; //where ds = dataset which contains your table
DataGrid1.DataMember = ds.Tables[0].TableName;
DataGrid1.DataBind();

//Silverlight
datagrid.ItemsSource = dataset.Tables[0].DefaultView;

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid.itemssource(v=vs.95).aspx

So in your case if you're using silverlight I suppose you can bind to the
t.DefaultView
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

680 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