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
Thank you for the clarification.
This behavior is happening because the ToString method of the decimal values keep the trailing zeros instead of trimming them (like the double ToString method does). You can remove the trailing zeros in the grid by using the IEditorDataFilter interface. It allows you to change the values displayed by the grid. For more information about the interface, please follow this link:
http://help.infragistics.com/Help/Doc/WinForms/2012.1/CLR2.0/html/WinGrid_Using_the_IEditorDataFilter_Interface_with_WinGrid.html
I have also attached a modified version of my sample, which demonstrates how to implement it.
Please let me know if you have any additional question.
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.