Hello,
I read a couple of posts about this, but wasn't able to find a satisfactory solution. I've read these docs referenced in the posts:
http://devcenter.infragistics.com/Support/KnowledgeBaseArticle.aspx?ArticleID=4877http://help.infragistics.com/Help/NetAdvantage/NET/2007.3/CLR2.0/html/WinGrid_Formatting_Cells_Containing_Specific_Number_of_Characters.html
but,
Andrea said:how can I use a UltraNumericEditor in a data binded grid cell?
You would use the EditorControl property of the column.
Andrea said:it's difficult to me to believe that I have to implement a percentage logic in a datafilter. Do I really have to?
It depends what you want to do.
Andrea said:instead of using a number of classes, interfaces and coding the trivial percentage concept, how can I follow the event way and manually manage the value-format of the number in the cell?
You could hide the "real" databound column in the grid and add an unbound column. Then you would use the InitializeRow event to populate the unbound column with a string based on the real column's value and use AfterCellUpdate to go the other way, copying the unbound column value to the "real" cell.
Thanks Mike. At last it worked well.
The only thing is that I wasn't able to add a '%' simbol at the end of the edit pattern to be shown in the control. I tried with
this.ultraNumericEditor1.MaskInput = "nnn\\%";
but the simbol dosn't appear.
However I had to tell to the DataFilter in my control to behave like a percentual, hence I tell it to multiply by 100 and divide by 100... This point sounded me strange. Why not provide some BasicDataFilters (e.g for percent, sexagesimal to decimal, and so on)?
I solved this problem using the grid's events.
Our grid is named ultraGridOptimizationBudgets and we have a control that flips the display format from "regular" numeric to percent.
I don't know if this is the most elegant solution, but here is my code, hope it helps:
Note how I use the cell's value going in and the text going out.
(PS. I am using summary values and had to alter the custom summary object to prevent the summary value from showing an incorrect total. That code is not shown here.)
private void ultraGridOptimizationBudgets_BeforeEnterEditMode(object sender, System.ComponentModel.CancelEventArgs e){ UltraGridCell activeCell = ((UltraGrid)sender).ActiveCell; if (cellIsInPercentMode(activeCell)) { decimal value = cellValue(activeCell.Value.ToString()); if (value != 0) { activeCell.Value = value * 100m; } }}
private void ultraGridOptimizationBudgets_BeforeExitEditMode(object sender, Infragistics.Win.UltraWinGrid.BeforeExitEditModeEventArgs e){ UltraGridCell activeCell = ((UltraGrid)sender).ActiveCell; if (cellIsInPercentMode(activeCell)) { decimal value = cellValue(activeCell.Text); if (value != 0) { activeCell.Value = value / 100m; } }}
private bool cellIsInPercentMode(UltraGridCell cell){ return (cell.Column.Format.Contains('%') || cell.Column.Format.Contains('p'));}
private decimal cellValue(string cellValue){ decimal value = 0; decimal.TryParse(cellValue, out value); return value;}
Hi Duhg,
i too had the same problem with percentage edit in the ultragridcell
i have followed your code and it worked fine for me.
am also displaying the summary values but the summary values are not displayed correctly .can you also send the SUMMARY DISPLAY code to display correct summary values
please can you do me this favour as soon as possible.its very urgent
Regards
Priyanka
Here is the code from the control that houses the grid. I have ripped out the stuff that is not related to the summaries.
private void ultraGridOptimizationBudgets_InitializeLayout(object sender, InitializeLayoutEventArgs e) { foreach (UltraGridBand band in e.Layout.Bands) { configureBandColumns(band); } } private void configureBandColumns(UltraGridBand band) { foreach (UltraGridColumn col in band.Columns) { createSummaryCell(band, col); } } private static void createSummaryCell(UltraGridBand band, UltraGridColumn col) { SummarySettings summary = band.Summaries.Add( col.Key, SummaryType.Custom, new CustomSummary(), col, SummaryPosition.UseSummaryPositionColumn, col); summary.Appearance.TextHAlign = HAlign.Right; }
private
void ultraGridOptimizationBudgets_InitializeLayout(object sender, InitializeLayoutEventArgs e) {
{
foreach (UltraGridBand band in e.Layout.Bands) { configureBandColumns(band); } }
configureBandColumns(band);
}
void configureBandColumns(UltraGridBand band) {
foreach (UltraGridColumn col in band.Columns) { createSummaryCell(band, col); } }
createSummaryCell(band, col);
static void createSummaryCell(UltraGridBand band, UltraGridColumn col) { SummarySettings summary = band.Summaries.Add( col.Key, SummaryType.Custom,
SummarySettings summary = band.Summaries.Add(
col.Key, SummaryType.Custom,
new CustomSummary(), col, SummaryPosition.UseSummaryPositionColumn, col); summary.Appearance.TextHAlign = HAlign.Right; }
col, SummaryPosition.UseSummaryPositionColumn, col);
summary.Appearance.TextHAlign = HAlign.Right;
I apologize for the layout of the code. The editor in this forum does not seem to be condusive to pasting code.
private void ultraGridOptimizationBudgets_InitializeLayout(object sender, InitializeLayoutEventArgs e)
foreach (UltraGridBand band in e.Layout.Bands)
private void configureBandColumns(UltraGridBand band)
foreach (UltraGridColumn col in band.Columns)
private static void createSummaryCell(UltraGridBand band, UltraGridColumn col)
col.Key, SummaryType.Custom, new CustomSummary(),
Custom summary class
using System;
using Infragistics.Shared;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
namespace NCS.UnitAllocation.OptimizerBudgets.Client
/// <summary>
/// CustomSummary
/// <remarks>This class is used to calculate the values in the summaries</remarks>
/// </summary>
public class CustomSummary : ICustomSummaryCalculator
private decimal _total = 0;
private bool _isSum = true;
private string _outputText = "Weekly Totals";
internal CustomSummary()
/// This method is from the ICustomSummaryCalculator interface
/// <param name="summarySettings"></param>
/// <param name="rows"></param>
public void BeginCustomSummary( SummarySettings summarySettings, RowsCollection rows )
// Begins the summary for the SummarySettings object passed in. Implementation of
// this method should reset any state variables used for calculating the summary.
if (summarySettings.SourceColumn.DataType.Name.ToLower() == "string")
_isSum = false;
else
_total = 0;
/// <param name="row"></param>
public void AggregateCustomSummary( SummarySettings summarySettings, UltraGridRow row )
// Here is where we process each row that gets passed in.
if (_isSum)
object cellValue = row.GetCellValue(summarySettings.SourceColumn);
// Handle null values
if (cellValue is DBNull)
return;
// Convert to decimal.
decimal nValue = Convert.ToDecimal(cellValue);
// This is for the percent-editing logic
// Are we the active cell and are we in percent mode and are we in edit mode?
UltraGridCell thisCell = row.Cells[summarySettings.SourceColumn.Key];
if (thisCell.IsActiveCell && thisCell.Column.Format.Contains("%") && thisCell.IsInEditMode)
nValue = nValue / 100;
_total += nValue;
/// <returns></returns>
public object EndCustomSummary( SummarySettings summarySettings, RowsCollection rows )
// This gets called when the every row has been processed so here is where we
// would return the calculated summary value.
if (!_isSum)
summarySettings.DisplayFormat = "{0}";
return _outputText;
if (_total > 0)
if (summarySettings.DisplayFormat.Contains("%"))
return Math.Round(_total, 2, MidpointRounding.AwayFromZero);
return Math.Round(_total, 0, MidpointRounding.AwayFromZero);
return null;
Hey Thanks For your reply
i need to put the code u sent and check whether it works fine.Thanks a ton
I too have been dealing with the same issue. I was able to successfully use the BeforeEnterEditMode and BeforeExitEditMode as you are doing (multiply by 100 when entering and divide by 100 upon exit).
The problem I am seeing though is with the formatting. In InitializeLayout, I am setting the column's Format property to be "#0.00%" so that I get a percentage value with 2 decimal places.
The UltraNumericEditor that I have associated with the column's EditorControl has it's MaskInput property set to "nnn.nn". This allows me to enter in values like 12.25 into the editor during edit mode. When I set a breakpoint in the BeforeExitEditModes, the value that I am placing in the active cell's value is 0.1225D (12.25 / 100 ) which seems correct. I would have expected the Format on the cell to then multiply by 100 since the format is "#0.00%, yielding 12.25%. Instead, it rounds the decimal places and I am left with 12.00% in the cell. Likewise, it I enter in 12.51, the result in the cell upon exit will be 13.00. When I enter back into edit mode, the values are 12 and 13 respectively.
What am I missing here?