Business Object Collection And DataGridView

C

Claude

Here's an homemade Attribut class :

public class Attribut
{
string m_sName;
object m_oValue;

public string Name { get { return m_sName;} }

public object Value { get { return m_oValue; } set { m_oValue = value; } }

public override string ToString()
{
if (m_oValue != null)
return m_oValue.ToString();
else
return "";
}
}

Here's a business object
public class MyObject
{
Attribut m_oAtt1;
Attribut m_oAtt2;
Attribut m_oAtt3;

public Attribut1 Att1 { get { return m_oAtt1;} set { m_oAtt1 = value; }}
...
}

Here's a collection of my business object
public MyObjectCollection : CollectionBase
{
....
}

I set the datasource of my DataGridView with my homemade collection.
Everything goes fine. The data is rendered correctly.

The problem happens when I try to modify, for example column 1 that
corresponds to Att1, an error occurs saying that the DataGridView can't
convert from System.String to MyAssembly.Attribut.

I know why it acts this way, but what I need to know is how can I bind the
sub-property Value of the more complex object Attribute in my class MyObject
in the DataGridView cell ? (So that the DataGridView can modify the Value of
the Attribute and not the Attribute object itself).

==> My english is a bit rusty, sorry.

Thanks,
Claude
 
I

Ignacio Machin ( .NET/ C# MVP )

Here's an homemade Attribut class :

public class Attribut
{
  string m_sName;
  object m_oValue;

  public string Name { get { return m_sName;} }

  public object Value { get { return m_oValue; } set { m_oValue = value; } }

  public override string ToString()
  {
     if (m_oValue != null)
       return m_oValue.ToString();
     else
       return "";
  }

}

Here's a business object
public class MyObject
{
   Attribut m_oAtt1;
   Attribut m_oAtt2;
   Attribut m_oAtt3;

  public Attribut1 Att1 { get { return m_oAtt1;} set { m_oAtt1 = value; }}
  ...

}

Here's a collection of my business object
public MyObjectCollection : CollectionBase
{
...

}

I set the datasource of my DataGridView with my homemade collection.
Everything goes fine. The data is rendered correctly.

The problem happens when I try to modify, for example column 1 that
corresponds to Att1, an error occurs saying that the DataGridView can't
convert from System.String to MyAssembly.Attribut.

I know why it acts this way, but what I need to know is how can I bind the
sub-property Value of the more complex object Attribute in my class MyObject
in the DataGridView cell ? (So that the DataGridView can modify the Value of
the Attribute and not the Attribute object itself).

==> My english is a bit rusty, sorry.

Thanks,
  Claude

Hi,

You cannot do it like that, as a matter of fact, the only reason why
the above code bind correctly is cause you overrided ToString().
You can provide wrappers properties in MyObject to do that :
public class MyObject
{
Attribut m_oAtt1;

string Attri1_Name{ get {return m_oatt1.Name;} set { moatt1.name =
value; }

Btw, you do not need to implement a cllection, just use List<T>
 
C

Claude

I knew you'd answer that... I'm sad it cannot work the way I want it
to work. Guess I have no choice than to duplicate each properties in my
object so I can bind correctly in the DataGridView.

Thanks,
Claude
 
I

Ignacio Machin ( .NET/ C# MVP )

I knew you'd answer that... I'm sad it cannot work the way I want it
to work. Guess I have no choice than to duplicate each properties in my
object so I can bind correctly in the DataGridView.

 Thanks,
    Claude

:






- Show quoted text -

Hi,

That is the easiest solution, otherwise you could implement some
interfaces like IBindingList or IDataSource
 
M

Marc Gravell

My first question would be: why do you think you need this extra
complexity? I've done a lot of work in this area, and *when needed* it
is fine - but it isn't good practice to do this "juts because".

There are 2 possible options here; the first thing to look at would be a
TypeConverter on Attribut, where ConvertFrom wraps the object and
ConvertTo unwraps it; a second option would be a custom type-descriptor
/ property-descriptor setup.

Neither of these are earth-shatteringly complex, but require a bit of
knowledge about System.ComponentModel.

However! Before you even start down any roads, I would challenge the
wisdom of the overall approach... it isn't very well typed throughout,
and the idea of the attribute *value* defining the attribute (name) is
just odd... plus I'm not sure how it even *gets* a name (there is no
setter etc).

I would also argue strongly for generics in a case like this - "object"
isn't going to do you any favours.
 

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