Solved

Binding Winform DataGrid

Posted on 2007-03-28
5
842 Views
Last Modified: 2008-02-01
Hi All,

What is the best way to bind a Winform DataGrid.  All my Data is coming through a webservice e.g I have CustomerResults[] array returned from the service method

What is best way with performance to bind to DataGrid.
Should I create a DataTable from CustomerResults[] array Collection and  Then bind it to the DataGrid. Or any other way?

Thanks in Advance
0
Comment
Question by:AdibRahman77
  • 3
5 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18811414
You can bind an array of objects DIRECTLY to the DataGrid's DataSource.
0
 
LVL 21

Assisted Solution

by:Yurich
Yurich earned 150 total points
ID: 18811421
There is a normal procedure of binding DataGrids, like

DataGrid.DataSource = ...
DataGrid.DataMember = ...

DataSource can be one of the following: DataTable, DataView, DataSet, DataManager. The easiest probably would be indeed producing a DataTable from your array and then binding it to the DataGrid.

Regs,
Yurich
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 225 total points
ID: 18811449
This here works (Myclass has a Name property, and was tested using VS2005, but should be ok in 2003 as well, except for the generic part):

            DataGrid dg = new DataGrid();
            Controls.Clear();
            Controls.Add(dg);
            List<Myclass > ls = new List<Myclass >();
            Myclass gp;
            gp = new Myclass ();
            gp.Name = "x1";
            ls.Add(gp);
            gp = new Myclass ();
            gp.Name = "x2";
            ls.Add(gp);
            gp = new Myclass ();
            gp.Name = "x3";
            ls.Add(gp);
            dg.DataSource = ls.ToArray();

So since we are binding an array directly to the datasource, I see NO NEED to do any conversion.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 18811461
In this case, of course, the Datagrid autogenerates the columns.  If you have columns that do NOT need to be displayed, you will have to turn off the autogenerate and list the ones you DO need.
0
 
LVL 10

Assisted Solution

by:jinn_hnnl
jinn_hnnl earned 125 total points
ID: 18814209
You can alway bind array of object to a datagrid. (but I prefer to bind it to dataTable - in case you wanna sort or do some anotherthing.... you can easily access to that (dataTable)dataSource to do such thing)

These are 2 good example
http://www.tutorialized.com/tutorial/How-to-bind-a-DataGrid-to-an-array/5748
http://support.microsoft.com/kb/315784
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

815 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

9 Experts available now in Live!

Get 1:1 Help Now