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
  • 469350
    Offline posted

    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.

Reply Children