This is a migrated thread and some comments may be shown as answers.

Format GridViewTextBoxColumn for currency

1 Answer 460 Views
GridView
This is a migrated thread and some comments may be shown as answers.
MJ14
Top achievements
Rank 1
MJ14 asked on 12 May 2010, 08:27 PM
I have a RadGridView... One of its columns is GridViewTextBoxColumn. The cells in this column are editable and hold currency values. The users are able to enter the values. But I want to restrict the users to be able to enter only 2 digits after the decimals. The editor should not let users to enter anything 2 digits after decimal, e.g., 123.45 is correct but 123.4567 in wrong.

This issue happens while entering the data and not while displaying it.

How can I achieve the above functionality... (Even if use GridViewDecimalColumn the problem stays. In anycase, I do not want to use GridViewDecimalColumn because GridSpinEditor doesn't work with the functionalities we want).

I am using following during grid intialization but it is not working.

 

private void radGridAdjustments_Initialized(object sender, EventArgs e)

 {

 

 

radGridAdjustments.Columns.Add(

new GridViewTextBoxColumn("AdjAmount"));

radGridAdjustments.Columns[

"AdjAmount"].FieldName = "AdjAmount";
radGridAdjustments.Columns["AdjAmount"]).FormatInfo = CultureInfo.CurrentCulture

 

radGridAdjustments.Columns["AdjAmount"].FormatString = "{0:c}";

radGridAdjustments.Columns[

"AdjAmount"]).FormatInfo.NumberFormat.CurrencyDecimalDigits = 2;
}

1 Answer, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 18 May 2010, 09:52 AM
Hello MJ14,

You can achieve the desired behavior by using GridViewDecimalColumn. You only need to set the DecimalPlaces property and subscribe to the TextChanging event. You can use the following code snippet:
GridViewDecimalColumn decimalColumn = this.radGridView.Columns["DecimalColumn"] as GridViewDecimalColumn;
decimalColumn.DecimalPlaces = 2;

private void radGridView_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    GridSpinEditor editor = this.radGridView.ActiveEditor as GridSpinEditor;
    GridViewDecimalColumn dataColumn = this.radGridView.CurrentColumn as GridViewDecimalColumn;
 
    if (editor != null && dataColumn != null && dataColumn.UniqueName == "YourDecimalColumn")
    {
        GridSpinEditorElement editorElement = editor.EditorElement as GridSpinEditorElement;
        editorElement.TextBoxItem.TextChanging -= new TextChangingEventHandler(TextBoxItem_TextChanging);
        editorElement.TextBoxItem.TextChanging += new TextChangingEventHandler(TextBoxItem_TextChanging);
    }
}
 
private void TextBoxItem_TextChanging(object sender, TextChangingEventArgs e)
{
    int index = e.NewValue.LastIndexOf('.');
 
    if (index > -1)
    {
        e.Cancel = e.NewValue.Substring(index).Length > 2;
    }
}

In the TextChanging event you need to prevent the value from changing if you type more than 2 digits after the dot char.

Sincerely yours,
Svett
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
MJ14
Top achievements
Rank 1
Answers by
Svett
Telerik team
Share this question
or