473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Binding DataGrid to DataTable with custom classes

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?
Nov 15 '05 #1
5 11169
Hi Dalibor,

Huh, I won't ask why are you doing this :) (putting all fields into one
column - if you put a field into column you could bind DataTable directly).
Anyway, you should implement IBindingList interface.

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

"Dalibor Kusic" <da**********************@zg.htnet.hr> wrote in message
news:hv********************************@4ax.com...
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?

Nov 15 '05 #2
Check out following article:
http://www.ftponline.com/vsm/2003_06/online/wagner/
Create Databinding Structures
Learn how to create middle-tier classes you can bind to WinForms UI
controls.
by Bill Wagner
It's easy to bind a .NET data source, such as a DataSet, to WinForms UI
elements. You can also create your own middle-tier classes that are as easy
to use in .NET UIs as the .NET collections. This requires more work, but it
pays off with a better design that's easier to maintain and extend. Your
users don't need to understand your database schema, because you bind UI
elements to the business objects they use. Inside the code, your middle tier
provides the translations between the normalized data layer and the UI
objects your users expect. I'll explain how WinForms elements get and set
bound data elements, and how to create your own classes that you can attach
to UI elements.

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
"Dalibor Kusic" <da**********************@zg.htnet.hr> schreef in bericht
news:hv********************************@4ax.com...
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?

Nov 15 '05 #3
Ok here is an update on the question....

public class Person {

public string name, address, age;

public Person(string s)
{
//Parsing "s" into the public fields
}

public override ToString()
{
return name+address+age;
}

}

I create a DataTable with a single column of type "Person".
When I populate a DataTable through the code and bind this DataTable
to a DataGrid, fields of that column show correct values through the
class's ToString() method, but they are NOT editable. How can I make
the data in the DataTable editable through the DataGrid?
I tried implementing IEditableObject interface on the Person class but
with no effect....
Nov 15 '05 #4
Dalibor,

If I understand your post, once you create a System.Data.DataTable and load
it. The data table is disconnected from your class. When you update the
Datagrid you are updating the bound object. Hence unless you update your
object with the grid data, the object will remain unchanged.

My suggestion would be to create a collection or an ArrayList then bind it
to the DataGrid.

Hope this helps.
--
Glen Jones MCSD

"Dalibor Kusic" <da***********@zg.htnet.hr> wrote in message
news:9o********************************@4ax.com...
Ok here is an update on the question....

public class Person {

public string name, address, age;

public Person(string s)
{
//Parsing "s" into the public fields
}

public override ToString()
{
return name+address+age;
}

}

I create a DataTable with a single column of type "Person".
When I populate a DataTable through the code and bind this DataTable
to a DataGrid, fields of that column show correct values through the
class's ToString() method, but they are NOT editable. How can I make
the data in the DataTable editable through the DataGrid?
I tried implementing IEditableObject interface on the Person class but
with no effect....

Nov 15 '05 #5
On 2004-01-03, Dalibor Kusic <da***********@zg.htnet.hr> wrote:
Ok here is an update on the question....

public class Person {

public string name, address, age;

public Person(string s)
{
//Parsing "s" into the public fields
}

public override ToString()
{
return name+address+age;
}

}

I create a DataTable with a single column of type "Person".
When I populate a DataTable through the code and bind this DataTable
to a DataGrid, fields of that column show correct values through the
class's ToString() method, but they are NOT editable.
You've got the wrong interpretation of what's happening. You seem to
think that you've got a DataTable of Person objects, and the DataGrid is
calling ToString() to display them, but that's wrong. In fact, when you
add the Person object to the DataTable, the DataColumn is calling the
ToString() function to get a string it knows how to store. DataColumns
can't hold arbitrary objects, they are quite limited in the types of
objects they know how to store (see the DataColumn.DataType docs for a
list).
How can I make
the data in the DataTable editable through the DataGrid?
I tried implementing IEditableObject interface on the Person class but
with no effect....


As someone else mentioned, you want to use something other than
a DataTable here, like an ArrayList.

--
David
dfoster at
hotpop dot com
Nov 15 '05 #6

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

Similar topics

5
by: K | last post by:
I created a collection which is derived from ArrayList and implements IBindingList and ITypedList. Then I bound the DataSource of a data grid into the collection. It could show up the data but...
1
by: Joe Jax | last post by:
I'm building my own version of a readonly DataTable, which needs to bind to various controls. My data table class has two main collections (CollectionBase instances): one of columns, one of rows,...
4
by: The Alchemist | last post by:
I am having a problem with a dynamically-generated Datagrid. It is important to point out that this problem does not exist with a design-time created Datagrid, but only with a dynamically generated...
3
by: Just D | last post by:
Hi, If anybody needs to show some data retrieved from the database table, what method is more preferrable? 1. DataSet ds = ...; DataGrid.Data.Source.ds; DataGrid.Bind(); 2. Write a custom...
8
by: Strahimir Antoljak | last post by:
I bound a DataGrid to a table (I tried with a DataView too). I removed the row from the table, and then inserted a new row at the same row index. the table gets the row to the right position, but...
8
by: Richard L Rosenheim | last post by:
I have a dataset containing a parent table related to a child table. The child table contains an ID field (which is configured as autonumber in the datatable), the ID of the parent, plus some...
3
by: Datatable Dataset Datagrid help | last post by:
Hi I am somewhat confused, I am new at VB.net I use XML data, I have a datagrid, I created a datatable so that I can create a custom format like true is this graphic false is this graphic and...
10
by: JohnR | last post by:
I have a datatable as the datasource to a datagrid. The datagrid has a datagridtablestyle defined. I use the datagridtablestyle to change the order of the columns (so they can be different than...
0
by: cindy | last post by:
I have a dynamic datagrid. I have custom classes for the controls public class CreateEditItemTemplateDDL : ITemplate { DataTable dtBind; string strddlName; string strSelectedID; string...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.