Numeric Format in Grid for .Net

Article ID: 749
Last updated: 05 Jan, 2012
Article ID: 749
Last updated: 05 Jan, 2012
Revision: 1
Views: 6677
Posted: 27 Dec, 2005
by Meltreger B.
Updated: 05 Jan, 2012
by Meltreger B.
Problem

How to format numeric cell with following requirements:

 

1. Format of the Numeric values can be set by the user at run time.

 User should be able to change the number of decimal places at run time. Example: If the user enters a value of 1000.00002 and current format setting for the decimal place is  = 2, then it should display as 1000.00, but  the cell should maintain the actual value of 1000.00002 .

2. Negative value:

When  a user enters a negative value the value should be represented in parenthesis.

Example: if user enters -22.22, it should become (22.22), but the actual value  stored should  not change.

3. Formatted value:

Grid value should display thousands separated by a comma.

Example: 10000 should be display as 10,000, but the actual stored value should  be unchanged.

4. Numeric value check:

User should be able to enter only numeric value, if he tries to enter some other character it should ignore that character.




Cause




Action

We have a Currency control that meets most of these requirements.

Changing the number of decimals displayed can be tricky, however, but this can be implemented in handlers for Start and End editing.

 

private void Form1_Load(object sender, System.EventArgs e)

{

      Style NumStyle = new Style();

      NumStyle.Control = Stingray.Grid.ControlType.Currency;

      NumStyle.set_UserAttribute(UserAttributeID.CURSEP,",.");

                NumStyle.set_UserAttribute(UserAttributeID.CURNUMDECIMALS,"4");

      NumStyle.set_UserAttribute(UserAttributeID.CURNEGFORMAT, "4");

      NumStyle.set_UserAttribute(UserAttributeID.CURMON, "0");

      gridControl2.SetStyleRange(Range.Cell(1,1),NumStyle);

      gridControl2[1,1].Style

.set_UserAttribute(UserAttributeID.CURNUMFRACT,"2");

      gridControl2[1,1].Style.Value = "1234.1234";

}

 

private void gridControl2_EndEditing(object sender,

Stingray.Grid.EndEditingEventArgs e)

{

      Style style = gridControl2[1,1].Style;

                  style.set_UserAttribute

                                (UserAttributeID.CURNUMFRACT,"2");

      gridControl2.SetStyleRange(Range.Cell(1,1), style);

}

 

private void gridControl2_StartEditing(object sender,

Stingray.Grid.StartEditingEventArgs e)

{

      Style style = gridControl2[1,1].Style;

      style.set_UserAttribute(UserAttributeID.CURNUMFRACT,"4");

      gridControl2.SetStyleRange(Range.Cell(1,1),style);

}


This article was:   Helpful | Not helpful
Report an issue
Article ID: 749
Last updated: 05 Jan, 2012
Revision: 1
Views: 6677
Posted: 27 Dec, 2005 by Meltreger B.
Updated: 05 Jan, 2012 by Meltreger B.

Others in this category