Hi Michael,
> Do you have control over the Book and Customer classes?
I guess sort of, they are POCO generated entites from Entity
FrameWork. Their code might get overridden.
Never tried implementing an interface in a partial class, but
extending the classes with Implementing an IDisplayInterface is way
preferable to "if else" or man handeling the logic manually.
Now somewhere I saw how to extend POCO entities, google will help me.
Thank You
Regards
Arjang
> if so you can add an interface to them and then get each class to implement
> it differently:
> interface IHaveDisplayName {
> string DisplayName { get; }
> }
> then you get:
> if(e.Value is IHaveDisplayName) e.Value =
> ((IHaveDisplayName)e.Value).DisplayName
> else e.Value = "Unknown"
> that requires some nasty type checking though. I assume e.Value is an
> object? if you're in .NET 4.0 you could use dynamic to choose a method to
> call on a separate object. Like this:
> class DisplayNameFactory
> {
> public string GetName(object o) { return "Unknown"; }
> public string GetName(Book book) { return book.Title; }
> public string GetName(Customer customer) { return customer.FullName; }
> }
> then in your handler you have:
> e.Value = displayNameFactory.GetName(e.Value as dynamic);
> Probably overkill and comes with a bunch of caveats** but it still works.
>
> ** For this to work: All of the methods must have the same name and take the
> same number of args in the same order (because of the dynamic lookup). You
> should have a method that accepts an object so that if the dynamic dispatch
> fails you get something meaningful
> --
> Michael M. Minutillo
> Indiscriminate Information Sponge
> Blog: http://wolfbyte-net.blogspot.com
>
>
> On Wed, Oct 27, 2010 at 9:59 AM, Arjang Assadi <[email protected]>
> wrote:
>>
>> I need to do something similar to do this :
>>
>> if (e.Value is Book)
>> {
>> e.Value = (e.Value as X).Title;
>>
>> }
>> else if (e.Value is Customer)
>> {
>> e.Value = (e.Value as Customer).FullName;
>> }
>> else
>> {
>> e.Value = "Unknown";
>> }
>> etc.
>>
>> in DataGridView_CellFormatting even, but it just looks plain ugly. is
>> there way to do this in a more structured manner?
>>
>> Regards
>>
>> Arjang
>
>