Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
235
Keyboard shortcut to change # of decimals in column
posted

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);
}
}
}
}

Parents Reply Children
No Data