Replies
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
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)
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