Solved

Binding Winform DataGrid

Posted on 2007-03-28
5
839 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

21 Experts available now in Live!

Get 1:1 Help Now