Solved

Binding Winform DataGrid

Posted on 2007-03-28
5
840 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

22 Experts available now in Live!

Get 1:1 Help Now