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.
Unfortunately, activation email could not send to your email. Please try again.

Limiting the number of decimal places

Thread ID:

Created:

Updated:

Platform:

Replies:

68595 Sep 25,2007 07:36 AM Oct 1,2007 03:35 AM Windows Forms 3
loading
Tags: GridControl
Geoff Parker
Asked On September 25, 2007 07:36 AM

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

Any ideas welcom

Ajish [Syncfusion]
Replied On September 25, 2007 09:27 PM

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.

Geoff Parker
Replied On September 27, 2007 12:24 PM

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.

Jeba S [Syncfusion]
Replied On October 1, 2007 03:35 AM

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.

CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;