v
Not logged inv
SearchAsk a QuestionReport an IssueMake a SuggestionMy Questions and Issues
Issue Details
Find By ID

TypeConverter attribute is ignored by XtraGrid (and probably other DX complex controls)

Issue Details

Log in to Track Changes or Edit
CS2436
Suggestion
Miha Markic - DXSquad
Yes
Processed
Rejected
.NET
XtraGrid Suite
12/26/2006 1:57:53 PM
-> Created by Miha Markic - DXSquad 12/7/2006 1:22:58 PM
Description
Proposed Solution

Consider these classes:
public class Tubo
    {
        private DateTime date;

        public Tubo(DateTime date)
        {
            this.date = date;
        }

        [TypeConverterAttribute(typeof(TuboConverter))]
        public DateTime Date
        {
            get
            {
                return date;
            }
            set
            {
                date = value;
            }
        }
    }

public class TuboConverter: TypeConverter
    {
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            return base.ConvertFrom(context, culture, value);
        }

        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            DateTime x = (DateTime)value;
            return x.AddHours(-1).ToString();
        }
    }

TypeConverter provided as the attribute is never used when I use BindingList<Tubo> as a datasource to XtraGrid. I believe it should be, just like DataGridView does.
Otherwise default .net behaviour is broken and one looses any chance to format the value.

<- Reviewed by DevExpress Team 12/13/2006 2:25:25 PM
<- Processed (Rejected) by DevExpress Team 12/26/2006 1:57:53 PM

Hello Miha,

Merry Christmas and a Happy New Year!

The desired functionality can be implemented via an ICustomFormatter class. Here is the necessary code:

[C#]

public class TuboFormatter : IFormatProvider, ICustomFormatter {
    public object GetFormat(Type formatType) {
        if(formatType == typeof(ICustomFormatter))
            return this;
        else
            return null;
    }
    public string Format(string format, object arg, IFormatProvider formatProvider) {
        DateTime x = (DateTime)arg;
        return x.AddHours(-1).ToString();
    }
}

...

this.gridView1.Columns[0].DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
this.gridView1.Columns[0].DisplayFormat.Format = new TuboFormatter();

It's also possible to create a more abstract formatter class, which will utilize the appropriate type converter for a given value. However, we advise against this solution, because it significantly impacts the grid's painting performance. That's the reason why we are not going to implement this feature in our XtraGrid. We've tested it and the cell painting performance has been dropped.

Another solution to your task is to implement the conversion in the getter and setter methods of your property:

[C#]

public string MyConvertedDateTime {
  get { this.Date.AddHours(-1).ToString() }
  set { this.Date = DateTime.Parse(value); }
}

The third solution is to create and use an unbound column for your data in the XtraGrid.

Thank you,
Nick

Log in to Track Changes or Edit

Peer-to-Peer Discussion in DevExpress Forums

No discussion on this article has been started yet.

Please login to start discussion.

v
v
Search
Searching Tips