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
close icon

Format Specifier

Hi,

we use SyncFusion Suite 4.2. I'm looking for a numerical format specifier for the grid control that allows me

- when the user enters an integer number to automatically add 2 decimals (to have '.00')
- to keep more decimals than two if the user enters more

Just a sample:

User enters 1 and 4.5567 then in the grid should be displayed 1.00 and 4.5567 using the same format specifier.

Any suggestions about that?

Thanks,
Christian

7 Replies

AD Administrator Syncfusion Team October 27, 2006 05:46 AM UTC

Hi Christian,

You can handle the DrawCellDisplayText event of the grid and set the e.DisplayText to some new value to want to display. Use the e.Style.CellIdentity.RowIndex and e.Style.CellIdentity.ColIndex to get the row and column of the cell for which the displaytext has to be set. Below is a code snippet.

private void grid_DrawCellDisplayText(object sender, GridDrawCellDisplayTextEventArgs e)
{
if(e.Style.CellIdentity.ColIndex == 1 && e.Style.CellIdentity.RowIndex > 0 && e.DisplayText != null && e.DisplayText != string.Empty )
{
string svalue = e.Style.CellValue.ToString();
double dvalue = Double.Parse(svalue);
int index = svalue.IndexOf(".",0);
if(index == -1 )
{
e.DisplayText = String.Format("{0:c}",dvalue) ;
}
else if( index + 2 >= svalue.Length )
{
e.DisplayText = String.Format("{0:c}",dvalue) ;
}
}
}
Let me know if this helps.

Best Regards,
Haneef


AD Administrator Syncfusion Team October 30, 2006 12:54 PM UTC

Hi Haneef,

thanks so far. The approach is good but doesn't work completely.

I still need a format string that formats double value (with thousand seperators) but keep all decimals BUT also add decimals if less than two entered, e.g.

3.1415 should be displayed 3.1415
30000.14 should be displayed 30,000.14
3.1 should be display 3.10
3 should be display 3.00 -> this works now with your sample

Or in other words: Which is the correct format specifier to show two decimals if less than two decimals have been entered and show all decimals that have been entered if more.

The sense of this is that a user should see the entire numbers he entered but the numbers should be formatted as far as possible and never any rounding should be done nor displayed. If the user enters no decimals this should be displayed with two decimals anyway.

Thanks,
Christian






















AD Administrator Syncfusion Team October 31, 2006 05:00 AM UTC

Hi Christian,

You can do with a standard TextBox cell with a numerical CellValueType and a properly set Format string. Below is a code snippet

this.gridControl1[1,1].CellValueType = typeof(decimal);
this.gridControl1[1,1].Format = "#,#0.00###";

Best Regards,
Haneef


RG Ronald Gouldner July 6, 2007 08:14 PM UTC

I am using this method to format my cell and it works great; however, it has a side effect I don't really like.

My format is "#.##%" which converts to a percent value rounded to 2 decimal places for display.

My problem is I want the result that I get to also round to the same decimal precision.

for example if I enter
.2345678

Then the Text method returns ".2345678"
the FormattedText method returns 23.46%

What I want to get is .2346 which represents the decimal value of the actual text the user winds up seeing.

Ideally I would like the decimal precision applied to my text string but I don't want formatting characters like % or , etc

>Hi Christian,

You can do with a standard TextBox cell with a numerical CellValueType and a properly set Format string. Below is a code snippet

this.gridControl1[1,1].CellValueType = typeof(decimal);
this.gridControl1[1,1].Format = "#,#0.00###";

Best Regards,
Haneef


HA haneefm Syncfusion Team July 6, 2007 08:45 PM UTC

Hi Ron,

You can try this.

this.gridControl1[1,1].CellValueType = typeof(decimal);
this.gridControl1[1,1].Format = "#.####";

Best regards,
Haneef


RG Ronald Gouldner July 6, 2007 09:03 PM UTC

I think I wasn't clear in my example. Sorry for that.

What I am trying to do is present the user with a % value when they enter a decimal. I want this % to have no decimal precision. I want the value that I then retrieve from the cell to be the decimal equivalent of the displayed value and not the original value entered but the user.

For example
User Entered Value->Displayed Value
.2->20%
1.2->120%
1.236->124% (note the rounding)
then I want to get the decimal values when I inspect the text for the cell.
20%->.2
120%->1.2
124%->1.24 (note the rounded value returned not the original 1.236)

Currently using a format of "#%" I get the correct displayed values. However I get the wrong value values when I get the text for the cell. I am getting the original value entered by the user 1.236 and not 1.24 in the last example. I don't like this because the user is left to think I have rounded the value when in fact I will use the original entered value.


>Hi Ron,

You can try this.

this.gridControl1[1,1].CellValueType = typeof(decimal);
this.gridControl1[1,1].Format = "#.####";

Best regards,
Haneef


HA haneefm Syncfusion Team July 9, 2007 11:27 PM UTC

Hi Ron,

You can handle the SaveCellInfo to store any changed values to the grid. Below is a code snippet to show this task.

private decimal GetValue(GridStyleInfo style)
{
string strCellValue = style.FormattedText.Replace("%", string.Empty);
decimal decCellValue = decimal.Parse(strCellValue);
return decCellValue / 100;
}

void gridControl1_SaveCellInfo(object sender, GridSaveCellInfoEventArgs e)
{
e.Style.CellValue = GetValue(e.Style);
}

Best regards,
Haneef

Loader.
Live Chat Icon For mobile
Up arrow icon