Convert array of objects into data grid

G

Guest

If I have an array of objects that I would like to display in a datagrid, what is the best way to convert the objects to a dataset to be used by the datagrid? Thanks!
 
K

Kevin Yu [MSFT]

Hi,

Thank you for using MSDN Newsgroup! My name is Kevin, and I will be
assisting you on this issue.

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to display an array of object
in a DataGrid and wonder what's the best way to achieve this. Please feel
free to point me out if there's any misunderstanding.

Based on my experience, I will create a DataSet, a DataTable with a column
to maintain data. The DataType property of the DataColumn will be set to
System.String. The following are .net framework types supported by
DataType. We cannot assign values other than these types to the field. If
the object array contains data other than these types, there might be some
problem when persisting and transfering data.

Boolean
Byte
Char
DateTime
Decimal
Double
Int16
Int32
Int64
SByte
Single
String
TimeSpan
UInt16
UInt32
UInt64

Here I write a short example for you. Hope this helps.

string s = "This is a string object.";
int i = 5;
DateTime dt = DateTime.Now;

object[] o = new object[]{s,i,dt};

DataSet ds = new DataSet();
ds.Tables.Add();
ds.Tables[0].Columns.Add("Column1"); //DataType will be set to String
by default.
foreach(object ob in o)
{
ds.Tables[0].Rows.Add(new object[]{ob});
}
this.dataGrid1.DataSource = ds;

For more information about DataColumn.DataType, please refer to the
following link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatacolumnclassdatatypetopic.asp

Does this answer your question? If anything is unclear, please feel free to
reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
M

Miha Markic

Hi Bob,

Another solution would be to implement a collection with IBindingList or
just IList.

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

Bob said:
If I have an array of objects that I would like to display in a datagrid,
what is the best way to convert the objects to a dataset to be used by the
datagrid? Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top