Current filter:
          You should refresh the page.
          Not Logged In

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

          0
            • 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.

          0

          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

          You must  log in  or  register  to leave an answer