Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
65
Ultragrid cell value format in edit mode
posted

Hi,

I’m trying to reproduce with an UltraGrid a behaviour we can reach with the standard Windows Forms DatagridView. With the standard grid there’s the possibility to override the methods GetFormattedValue and ParseFormattedValue in order to display/parse values with a custom behaviour:

 

        protected override object GetFormattedValue(object value, int rowIndex,

            ref DataGridViewCellStyle cellStyle,

            TypeConverter valueTypeConverter,

            TypeConverter formattedValueTypeConverter,

            DataGridViewDataErrorContexts context)

        {

            if (value != null && value != DBNull.Value)

            {

                if (this.RowIndex == -1 || !this.IsInEditMode)

                    value = FixDecimalPlaces(value, cellStyle);

            }

            return base.GetFormattedValue(value, rowIndex, ref cellStyle, valueTypeConverter, formattedValueTypeConverter, context);

        }

      

 public override object ParseFormattedValue(object formattedValue,

            DataGridViewCellStyle cellStyle,

            TypeConverter formattedValueTypeConverter,

            TypeConverter valueTypeConverter)

        {

            object ret = null;

           

                ret = base.ParseFormattedValue(formattedValue, cellStyle,

                formattedValueTypeConverter, valueTypeConverter);

 

            if (ret != null && ret != DBNull.Value)

            {

                ret = FixDecimalPlaces(ret, cellStyle);

            }

            return ret;

        }

 

Where FixDecimalPlaces is a function that rounds or truncates the cell value based on the specific column. This also let us show a truncated value in edit mode. Let’s assume the underlying value is 12.00000 (coming from a dataset, type decimal). The cell shows the formatted value 12.00 also entering in edit mode.

Using the Infragistics UltraGrid the value 12.00 becomes 12.00000 in edit mode. Is there a way to format the value in edit mode? We would not use masks if possible.

Best Regards,

Massimo Franzosi

Parents
No Data
Reply
  • 7695
    Offline posted

    Hi Massimo,

       There are a number of different ways you could possibly achieve the behavior, one of which would be by using a mask. But I am assuming by your intent, you do not care about or do not want to retain the precision. Because while you say the value 12.00 becomes 12.00000 that is not entirely true. 12.00000 is a distinctly different value then 12.00 and 12.00000 is what was stored. If you don't care about the precision, just cut the precision out. Such as in the BeforeEnterEditMode event, truncate the value.
    http://stackoverflow.com/questions/1132765/adjusting-decimal-precision-net

    If you want to retain the precision, but only if the text hasn't changed that where it can get tricky. I would suggest a IEditorDataFilter, but you could probably more easily do it via caching the original value in BeforeEnterEditMode, then in BeforeExitEditMode, compare the values, if the value coming back equals the truncated cached value, reset the value back to the cached value.

    Let me know if that helps,

Children
No Data