Luis,
You want to bind a column in the grid to "T" property of "B", right?
It's possible using custom ITypedList implementation and a class
derived from PropertyDescriptor. I wrote a sample for you. It binds
one column of the grid to "Foo.Name" and the second -- to
"Foo.Bar.Name". It's fully working, have a look.
Using this approach you can "extend" your basic object with whatever
bindable properties you might need. I use it for example to
concatenate FirstName and LastName properties of my business object
into single /editable/ Full Name column of the data grid, or to
concatenate PhoneNumbers child collection into comma separated list of
values.
The objects must be in a collection though, and you mentioned the
array. If you must use the array then you'll need to write a wrapper
which implements IList in addition to ITypedList.
Disclaimer: The project I'm using it in hasn't reached production
stage yet and therefore I can't say it's well tested...
Alexander
=============================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace GridCustomBinding
{
public class AppStart
{
[STAThread]
static void Main()
{
DataGrid g = new DataGrid();
DataGridTableStyle t = new DataGridTableStyle();
t.MappingName = "Foo";
DataGridColumnStyle name = new DataGridTextBoxColumn();
name.MappingName = "Name";
t.GridColumnStyles.Add(name);
DataGridColumnStyle barName = new DataGridTextBoxColumn();
barName.MappingName = "Bar.Name";
t.GridColumnStyles.Add(barName);
g.TableStyles.Add(t);
g.DataSource = BindableCollection.GetSampleData();
g.Dock = DockStyle.Fill;
Form f = new Form();
f.Controls.Add(g);
Application.Run(f);
}
}
public class BindableCollection : CollectionBase, ITypedList
{
public static BindableCollection GetSampleData()
{
BindableCollection col = new BindableCollection();
col.List.Add(new Foo());
col.List.Add(new Foo());
PropertyDescriptor extraProp = new FooBarPropDescriptor();
col.PropertyDescriptors.Add(extraProp);
return col;
}
#region ITypedList Members
private PropertyDescriptorCollection _props = null;
public PropertyDescriptorCollection PropertyDescriptors
{
get
{
if (_props == null)
{
// Collection returned from GetProperties
doesn't allow adding new elements
_props = new
PropertyDescriptorCollection(null);
foreach (PropertyDescriptor prop in
TypeDescriptor.GetProperties(typeof(Foo)))
{
_props.Add(prop);
}
}
return _props;
}
}
public PropertyDescriptorCollection
GetItemProperties(PropertyDescriptor[] listAccessors)
{
return PropertyDescriptors;
}
public string GetListName(PropertyDescriptor[] listAccessors)
{
// DataGrid uses this as MappingName
return "Foo";
}
#endregion
}
public class FooBarPropDescriptor : PropertyDescriptor
{
// The parameter in base constructor is the
// string you're going to use as MappingName property
// of a grid column
public FooBarPropDescriptor() : base("Bar.Name", null)
{}
public override object GetValue(object component)
{
Foo obj = (Foo) component;
return obj.Child.Name;
}
public override void SetValue(object component, object value)
{
Foo obj = (Foo) component;
obj.Child.Name = (string) value;
}
public override bool CanResetValue(object component)
{
return false;
}
public override void ResetValue(object component)
{
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
public override bool IsReadOnly
{
get
{
return false;
}
}
public override Type ComponentType
{
get
{
return typeof(Foo);
}
}
public override Type PropertyType
{
get
{
return typeof(string);
}
}
}
public class Foo
{
public string Name
{
get
{
return "Foo object";
}
}
private Bar _child = new Bar();
public Bar Child
{
get
{
return _child;
}
}
}
public class Bar
{
private string _name = "Bar object";
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
}
}
On Tue, 28 Sep 2004 18:21:10 +0100, Luis Abreu <[EMAIL PROTECTED]> wrote:
> Hello.
>
> ok, suppose you have class A, which has a property named classb of
> type ClassB. ClassB also has a property named T.
>
> class A
> {
> public ClassB classB {...}
> }
>
> class B
> {
> public string T;
> }
>
> Now let's suppose that I have an array or a collection of elements of
> type A. Is there a way for me to bind the subproperty B to a datagrid
> column by using the MappingName property of the datagridtextboxcolumn?
>
> thanks
> --
> Regards,
> Luis Abreu
> email: labreu_at_gmail.com
> http://weblogs.pontonetpt.com/luisabreu
> http://www.pontonetpt.com
> http://weblogs.pontonetpt.com/
>
> ===================================
> This list is hosted by DevelopMentor� http://www.develop.com
> Some .NET courses you may be interested in:
>
> Essential .NET: building applications and components with CSharp
> August 30 - September 3, in Los Angeles
> http://www.develop.com/courses/edotnet
>
> View archives and manage your subscription(s) at http://discuss.develop.com
>
===================================
This list is hosted by DevelopMentor� http://www.develop.com
Some .NET courses you may be interested in:
Essential .NET: building applications and components with CSharp
August 30 - September 3, in Los Angeles
http://www.develop.com/courses/edotnet
View archives and manage your subscription(s) at http://discuss.develop.com