Is there a way to have a decimal column that does not display the trailing zeros when editing a value?
I tried changing the Editor to an UltraNumericEditor but it keeps including mask characters and a $ that I do not want.
I just simply want the raw decimal number without the trailing zeros.
I see in the past it has been said that it is not possible, without some funky workarounds at which point it would be better to just force the column type to be double. (This will also be a pain for me).
Note that I am using currently Infragistics 2013 Volume 1.
I appreciate any help that is offered.Thank you.
Hi,
Thank you for posting in our forums.
By default the grid won’t show any trailing zeros when editing even if the column is decimal type. I have attached a small sample project. Notice that when you edit the Price column (which is of type Decimal) no trailing zeros are shown. What settings are applied to the decimal column in your application? Have you specified MaskInput or Format for the column?
Please provide me with a sample which reproduces this and I will be glad to research it further. You can use my sample as a starting point.
I am looking forward to hearing from you.
When I tried your sample application, I was easily able to have zeros in the Price Column. If I change a price to a different value with trailing zeros, they show when editing is complete and when editing the value again. For me, all of the data comes from a database (including the decimal qty field), so it appears to come with the zeros embedded.
Changing the first line of the data loading code in Form1_Load in your sample project to the following will demonstrate the issue (it is wrapped to ensure visibility):
ds.Tables["table1"].Rows.Add(new Object[]
{ null, "Beverage", 50, 10.200m, new DateTime(2008, 04, 22) });
Notice the change from "10.2" to "10.200m" which ensures that a decimal number is being pushed into the datatable.
I appreciate you looking into this.Thank you
That does appear very helpful, however, I have a concern about loss of precision due to actually calculating values with that number.
Since I know that my decimal numbers have a 14 decimal place precision as that how it is in the database, I have modified your example to force that precision and it appears to work.
I changed the code in CustomDataFilter.cs to the following (notice that I eliminated the "public decimal Normalize" method):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Infragistics.Win; namespace WG_EditWithNoTrailingZeros { class CustomDataFilter : IEditorDataFilter { public object Convert(EditorDataFilterConvertArgs conversionArgs) { object value; decimal decimalValue; switch (conversionArgs.Direction) { case ConversionDirection.EditorToDisplay: value = conversionArgs.Value; if (value != null && value is decimal) { decimalValue = (decimal)value; conversionArgs.Handled = true; conversionArgs.IsValid = true; // You can also use decimalValue.ToString("G29"), but if you have values // like the one in the second row it will cause problems return (decimalValue / 1.000000000000000000000000000000000m).ToString(); } break; case ConversionDirection.EditorToOwner: value = conversionArgs.Value; if (value != null && value is string && decimal.TryParse(value.ToString(), out decimalValue)) { conversionArgs.Handled = true; conversionArgs.IsValid = true; return (decimalValue * 1.00000000000000m).ToString(); } break; } return conversionArgs.Value; } } }
I also added a handler for the ClickCell event to test the results:
private void ultraGrid1_ClickCell(object sender, Infragistics.Win.UltraWinGrid.ClickCellEventArgs e) { Console.WriteLine(ultraGrid1.ActiveCell.Value.ToString()); }
Please let me know if there are any issues with this, or that it should behave as expected. I believe that it will get me the results I desire.
I do appreciate your help with this.
Thank you.
(Note I can post the entire project if desired)
Thank you for the reply.
After reviewing the code you have provided, it seems to me that it won’t cause issues with the UltraGrid. After testing the modified DataFilter, it seems that it ensures that each value will have at least 14 digits after the decimal point. As I don’t know the bigger picture in your application, I will leave it up to you to decide if this satisfies your goal. However with regards to the UltraGrid it shouldn’t cause any issues.
Please let me know if you have any additional questions.
I tried to apply your suggested modifications in my code, but unfortunately I ran into a few issues.
1. The Column Formatting was being ignored. Trying to manually apply the formatting was problematic for me at best.
2. My project is actually in VB.Net, and as such the precision conversion was not working since it is auto stripping the ZEROS out of the decimal number (a bug I think) Notice I used String manipulation instead (TrimEnd, etc.).
I created this and it seems to do what I want. I tried to use the OnBeforeEnterEditMode, but any attempt I made to set the text box text, was overridden by the system.
If you have any suggestions on how to do this better, I would greatly appreciate it. If everything is pretty much as good as it gets, let me know too please.
Thank You for all of your help with this.
Seradex.
Friend Class CustomDecimalEditorWithText Inherits EditorWithText Protected Overrides Sub OnAfterEnterEditMode() Me.SetTextBoxText(CType(Me.DataFilter, CustomDecimalDataFilter). ConvertValueToEditor(Me.Value), True) MyBase.OnAfterEnterEditMode() End Sub End Class Friend Class CustomDecimalDataFilter Implements IEditorDataFilter Public Function Convert(ByVal conversionArgs As EditorDataFilterConvertArgs) As Object Implements IEditorDataFilter.Convert Dim value As Object Dim decimalValue As Decimal Dim ReturnValue As String Select Case conversionArgs.Direction Case ConversionDirection.EditorToOwner value = conversionArgs.Value If value IsNot Nothing AndAlso TypeOf value Is String AndAlso Decimal.TryParse( value.ToString(), decimalValue) Then conversionArgs.Handled = True conversionArgs.IsValid = True If decimalValue.ToString.TrimEnd("0"c, "."c).Contains(".") Then Dim DecimalsLocation As Integer DecimalsLocation = decimalValue.ToString.IndexOf(".") + 1 ReturnValue = decimalValue.ToString.Substring(0, DecimalsLocation) & decimalValue.ToString.Substring(DecimalsLocation).PadRight(14, "0"c) Else ReturnValue = decimalValue.ToString & ".00000000000000" End If Return ReturnValue End If End Select Return conversionArgs.Value End Function Public Function ConvertValueToEditor(value As Object) As String Dim decimalValue As Decimal If value IsNot Nothing AndAlso TypeOf value Is Decimal Then decimalValue = DirectCast(value, Decimal) Return decimalValue.ToString.TrimEnd("0"c, "."c) End If Return value.ToString End Function End Class
I have used your DataFilter with the sample I have provided and it seems to me that it works fairly well. The only thing I would change is the ConvertValueEditor method the following way:
Public Function ConvertValueToEditor(value As Object) As String
Dim decimalValue As Decimal
If value IsNot Nothing AndAlso TypeOf value Is Decimal Then
decimalValue = DirectCast(value, Decimal)
Dim returnString = decimalValue.ToString()
If (returnString.Contains(".")) Then
Return returnString.TrimEnd("0"c).TrimEnd("."c)
Else
Return returnString
End If
Return value.ToString
End Function
The previous version can trim a number like 340.0 to just 34, using two consecutive EndTrims ensures that it won’t happen. Other than that everything seems fine for me.
Nice catch, Thank you for all your help.