Binding DataGrid to DataTable with custom classes

D

Dalibor Kusic

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?
 
M

Miha Markic

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.
 
J

Jan Tielens

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.
 
D

Dalibor Kusic

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....
 
G

Glen Jones MCSD

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.
 
D

David

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.
 

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