?
Solved

Binding Winform DataGrid

Posted on 2007-03-28
5
Medium Priority
?
862 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
[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
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 450 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 675 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 375 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

770 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