473,657 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using a DataGrid with something other than a DataTable

Hi,

I have some data which is stored as an array (or possibly an arraylist) of
identically typed objects, each of which has certain public properties. I
want to display these in a datagrid control.

According to the documentation, the datasource of a datagrid can be anything
which implements IEnumerable (which includes array and arraylist). However,
every example I've seen for datagrid uses a DataTable to store the data.

is there any way to achieve this?

here's a sample code snippet which doesn't work but I hope it illustrates
what I'm trying to do:

class person {
string firstname;
string lastname;
}

person[] people = GetListOfPeople ();

DataGrid.dataSo urce = people;
DataGrid.column s.add(new BoundColumn("fi rstname"))
DataGrid.column s.add(new BoundColumn("la stname"))
DataGrid.DataBi nd();

many thanks

Andy
Nov 18 '05 #1
2 1144
Yes, I do this a lot in my applicaiton.
The only difference that I can see to your code is that I set the columns
inside the <asp:datagrid > tag in the design
I suggest you try your code it might work :)

cheers,
mortb

"Andy Fish" <aj****@blueyon der.co.uk> wrote in message
news:ey******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

I have some data which is stored as an array (or possibly an arraylist) of
identically typed objects, each of which has certain public properties. I
want to display these in a datagrid control.

According to the documentation, the datasource of a datagrid can be anything which implements IEnumerable (which includes array and arraylist). However, every example I've seen for datagrid uses a DataTable to store the data.

is there any way to achieve this?

here's a sample code snippet which doesn't work but I hope it illustrates
what I'm trying to do:

class person {
string firstname;
string lastname;
}

person[] people = GetListOfPeople ();

DataGrid.dataSo urce = people;
DataGrid.column s.add(new BoundColumn("fi rstname"))
DataGrid.column s.add(new BoundColumn("la stname"))
DataGrid.DataBi nd();

many thanks

Andy

Nov 18 '05 #2
aha - this is what I had missed.

I was using public fields (sorry - I missed the public off my code snippet).
when I put in some property accessors it worked.

Unfortunately it will be a bit of a PITA as the objects I'm trying to
display don't have accessors so I will have to wrap them in another object
:-(

In the meantime I have also discovered template columns which I think may
allow me to access the properties directly. So hopefully I have a couple of
workable options.

Thanks for your help

Andy
"Bharat Biyani" <Bh**********@d iscussions.micr osoft.com> wrote in message
news:FA******** *************** ***********@mic rosoft.com...
Hi Andy,

To make the object bindable to the datagrid u will need to expose the
variables of the class as the properties of the Person object. Then u will be able to bind the properties. Here is a code fragment which will help u make things clearer.
Set the AutoGenerateCol umns property of datagrid to false.

Person[] p =new Person[2];
p[0]=new Person("John"," Wright");
p[1]=new Person("John"," abraham");
DataGrid1.DataS ource=p;
BoundColumn bc=new BoundColumn();
bc.DataField="F Name";
bc.HeaderText=" First Name";
DataGrid1.Colum ns.Add(bc);
bc=new BoundColumn();
bc.DataField="L Name";
bc.HeaderText=" Last Name";
DataGrid1.Colum ns.Add(bc);
DataGrid1.DataB ind();

---
Bharat Biyani (bs*@orcim.com)
http://www.orcim.com
"Andy Fish" wrote:
Hi,

I have some data which is stored as an array (or possibly an arraylist) of identically typed objects, each of which has certain public properties. I want to display these in a datagrid control.

According to the documentation, the datasource of a datagrid can be anything which implements IEnumerable (which includes array and arraylist). However, every example I've seen for datagrid uses a DataTable to store the data.

is there any way to achieve this?

here's a sample code snippet which doesn't work but I hope it illustrates what I'm trying to do:

class person {
string firstname;
string lastname;
}

person[] people = GetListOfPeople ();

DataGrid.dataSo urce = people;
DataGrid.column s.add(new BoundColumn("fi rstname"))
DataGrid.column s.add(new BoundColumn("la stname"))
DataGrid.DataBi nd();

many thanks

Andy

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
11189
by: Dalibor Kusic | last post by:
The DataTable contains, for example, only one column of lets say Person class. Person class has three fields, and a constructor that takes a single string and parses it over those three fields. Now, how do I modify the Person class (which interfaces to implement) so I can bind my DataTable to a DataGrid and then add, modify and delete records through it?
0
3171
by: Emerson | last post by:
The following assumes a System.Windows.Forms.DataGrid with a System.Data.DataTable set as the DataSource. I'm programming in C# Here's my scenario I click in a cell on a DataGrid. I enter some text. Before changing to another cell in the DataGrid, I move my mouse pointer off the DataGrid and change the selected item in a ListBox or TreeView. Here's my question Which should fire first, the DataTable's RowChanged/CellChanged event or...
2
2948
by: VM | last post by:
When I display data to a Windows datagrid I usually fill the underlying table (in another class) and then, once it contains all the data, I attach it to the grid. But there are some processes that involve large amounts of data so the user won't be able to see anything in the grid until after the table's already filled up (usually 40-60 secs with these large processes). Is it possible (without changing the process of filling the table...
4
25364
by: Wes | last post by:
I want to use a DataGrid and manually populate the data in the table. Does anyone know how this is done?? I want to create all the Columns, and then just add rows as my Program runs. Is there something else I should be using in C# than a DataGrid. Thanks for the Help
11
1694
by: Fred Nelson | last post by:
I have an application in which it would be VERY beneficial if I could obtain the names of the colums in a datagrid. For example dim datagrid1 as new datagrid datagrid1.datasource = (stored procedure that loads datagrid) datagrid1.databind() I am able to obtain the data in the datagrid by its relative number however
4
2279
by: Jan Nielsen | last post by:
Hi all I'm a former Access developer who would like to implement a many-to-many relation in about the same way you do in Access: With a subform and a combo box. Is it possible to use a combobox in a datagrid? Any other suggestions/articles on how to implement many-many relations in the frontend (which of course are 2 one-many relations)?
12
1775
by: Art | last post by:
Hi, I need help -- again -- I have a DataGrid with a DataTable as its source. It displays wonderfully when I put in some test data (withinin the code). Now, I'd like to change one of the values in the Grid, and have the DataTable change. I've tried using a Validating (and Validated) event to display the changed data. It doesn't change. I've tried adding
10
2334
by: Nick | last post by:
Hello, Please pardon my ignorance as I'm sure this is easy to do. I have a datagrid where I want to let the user delete columns. I added a context menu to the datagrid that has a delete option. If the user right clicks on a column heading can I highlight that column and then delete it? I'm not exactly sure how to highlight it or figure out what column has been clicked. In my testing so far when I use the context menu I always get the...
5
2323
by: Doug Bell | last post by:
Hi I have a DataGrid that has a DataView as its DataSource. I need to detect when a New Row is added or when a Row is Deleted so that I change data in the underlying DataTable. I found that MyDataGrid_CurrentCellChanged doesn't fire on a deletion or on a New Row if there was no previous rows. Can someone advised the simplest way to detect row additions or deletions?
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8820
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.