I wrote some code to add some easy ability to change the number format of the columns in the ultragrid.
However, my current solution only works if the format was originally specified as "N2" or "D0" or similar. How do I do the same thing if the column's number format is "0.0000;-0.0000;-"? Is there an easier way, or do i need to parse all 3 (pos/neg/zero) conditions separately and determine if they have a period or not, etc, to figure out what to change it to?
Thank you.
private string FormatChangeDecimal(string FormatString, int IncreaseInDecimals) { if (FormatString.Length == 2) { return FormatString[0] + Math.Max(0,(int.Parse(FormatString[1].ToString()) + IncreaseInDecimals)).ToString(); } else { return FormatString; } }
// Add Excel-style number formatting with avery extensions. if (e.Control == true && (e.Shift == true || e.KeyValue == 68)) { if (base.ActiveCell != null) { UltraGridColumn c = base.ActiveCell.Column; if (c != null) { string formatoverride = null; if (e.KeyValue == 49) { formatoverride = "N0"; } else if (e.KeyValue == 50) { formatoverride = "N0"; } else if (e.KeyValue == 51) { formatoverride = "d"; } else if (e.KeyValue == 52) { formatoverride = "C1"; } else if (e.KeyValue == 53) { formatoverride = "P2"; } else if (e.KeyValue == 68) { formatoverride = FormatChangeDecimal(c.Format, (e.Shift == true) ? 1 : -1); }
if (formatoverride != null) { if (_columnNumberFormatOverrides.ContainsKey(c.Header.Caption)) { _columnNumberFormatOverrides[c.Header.Caption] = formatoverride; } else { _columnNumberFormatOverrides.Add(c.Header.Caption, formatoverride); } c.Format = formatoverride; c.PerformAutoResize(PerformAutoSizeType.AllRowsInBand, AutoResizeColumnWidthOptions.IncludeCells); } } } }
Hi,
I'm not sure I am following you. Why does the original format of the column matter? There's certainly nothing in the grid that will prevent you from setting the Formation on the column, regardless of what the format was previously.
So if your code is not setting the format when it's not 2 digits, then that's because you coded it that way.
Are you asking for some way to identify that the format the grid is using is similar to the one you want? If so, the answer to your question is no, there's no way to do that other than to parse the string yourself.
The grid doesn't really know or care about the format you apply. It doesn't process it in any way. All it does is call the ToString method on the cell's Value and pass along the format to that method. So it's all handled by the DotNet framework.
I doubt there is something built-in to UltraGrid that supports this, but I'm throwing the question out there.
Basically, there are grids that have a lot of data, and the formats are chosen by algorithm. Sometimes the user wants to see more or less decimal places than chosen by algorithm. In this case, i want the user to just hit CTRL+D to add a decimal place in the active column and CTRL+SHIFT+D to remove a decimal place. Since this sort of functionality is most likely to exist in derived classes of UI grid controls, I am asking if anyone has already done this or already written a function of the form:
public string FormatIncreaseDecimal(string numberFormat, int numAdditionalDecimals)
such that the result of FormatIncreaseDecimal("#,##0.00;-#,##0.00;zero", -1) == "#,##0.000;-#,##0.000;zero"
Hi Robert,
Okay, I understand. So you need to detect the original format in order to determine how (or even if) to modify it. As I explained, the grid has no real knowledge of the format or how it will be applied, though. It's pretty much just opaque data as far as the grid is concerned. It gets passed on to the ToString method. So you would have to examine and parse the strings yourself.