We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Limiting the number of decimal places

I am struggling to set the maximum number of decimal places on a cell or column of cells.

Any ideas welcom

3 Replies

AJ Ajish Syncfusion Team September 26, 2007 01:27 AM UTC

Hi Geoff,

The number of decimal places in a cell can be limited by using appropriate cell format. Cell Format can be set using QueryCellInfo event handler. Here is the code for doing it,


void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if(e.Style.CellValueType == typeof(double))
e.Style.Format = "#.############";
}

kindly try the above and let me know if this helps.

Regards,
Ajish.


GP Geoff Parker September 27, 2007 04:24 PM UTC

Thanks for your response,

I still cant dont quite get it.

1) How do I set the cell to be a double.

Geoff


>Hi Geoff,

The number of decimal places in a cell can be limited by using appropriate cell format. Cell Format can be set using QueryCellInfo event handler. Here is the code for doing it,


void gridControl1_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
{
if(e.Style.CellValueType == typeof(double))
e.Style.Format = "#.############";
}

kindly try the above and let me know if this helps.

Regards,
Ajish.


JS Jeba S Syncfusion Team October 1, 2007 07:35 AM UTC

Hi Geoff,

Thank you for your update.

If you want to limit the decimal plases on each key stroke, then handle CurrentCellValidateString. In
your handler test the e.Text to see if it is valid with repsect to your requirements. If it is not, set
e.Cancel to true.If you do not want to do this for each keystroke, but only when the user tries to leave

the cell, then handle the CurrentCellValidating event. In it, get the text from grid.CurrentCell.ControlText, and test it, setting e.Cancel if it is bad.

Below is code that checks for a number in column 1 that is at most 5 places to the left of the decimal, and 2 places to the right of the decimal. You may have to tweak this code, but it should give you the idea. It will not allow you to type an invalid character.

Please refer this code snippets:

private void

gridControl1_CurrentCellValidateString(object sender, GridCurrentCellValidateStringEventArgs e)
{
GridCurrentCell cc = this.gridControl1.CurrentCell;
if (cc.ColIndex != 1)
{
return;
}

try
{
if (e.Text != ".")
{
double d = double.Parse(e.Text);
}
}
catch
{
e.Cancel = true;
return;
}
int dec = e.Text.IndexOf('.');
int len = e.Text.Length;
if (len > 8)
{
e.Cancel = true;
}
else if (dec > 4)
{
e.Cancel = true;
}
else if (dec == -1 & len > 5)
{
e.Cancel = true;
}
else if (dec > -1 & dec < len - 3)
{
e.Cancel = true;
}
}
}


Please refer the sample which implements the above said feature:
http://websamples.syncfusion.com/samples/Grid.Windows/F68595/main.htm

Kindly let us know if you need any further assistance.

Thank you for using Syncfusion Products.

Regards,
Jeba.

Loader.
Live Chat Icon For mobile
Up arrow icon