Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Binding Winform DataGrid

Posted on 2007-03-28
5
Medium Priority
?
868 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

618 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