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

Decimal Column Edit Value

3 Answers 250 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 28 Feb 2013, 01:49 PM
Hi,

I have a column in a grid column as a GridViewDecimalColumn where DecimalPlaces = 2, FormatString = "{0:#####,###,###,###,##0.##}"

This displays data correctly in all cases.

I am getting inconsistent behaviour though when I edit the cell depending on how the edit is started. This is a little difficult to explain, however :

Scenario 1 :

If I click in the cell so as to invoke the editor, the current value is displayed and fully selected and I am able to enter new data. The CellValueChanged event fires when I leave the cell if the data is valid. When CellEndEdit fires, it either has the new valid data, or the old value if invalid.

Scenario 2 :

If I highlight the cell but do not click to edit and press space, the same happens as above as the space keypress invokes the edit.

Scenario 3 :

If I just highlight the cell but do not click to enter edit mode and start typing, then cell editing begins. The CellValueChanged event fires immediately after the first keypress and the new value in this event is the value I typed, so If for example I enter "1", I now though get the following text in the cell 

"1.00"

and the CellValueChanged value = 1

I can now enter text, however if I hit the decimal point and 2, I get a duplicate decimal point entered, so the cell contents are now "1.2.00" which is invalid.This happens for all scenarios and as the data is not valid I get no CellValueChanged event fired when I exit the cell. In scenarios 1 and 2, in CellEndEdit, when the new data is invalid, the event argument contains the original cell value before the edit. In scenario 3 though, as the CellValueChanged event fired on initial keypress, CellEndEdit now fires with 1 and not the old value.

I am looking really for a way to prevent entering multiple decimal points, or apply an edit mask to the cell to prevent invalid data entry. Also, I would ideally be looking for consistent behaviour for the 3 scenarios, so for the grid to reinstate the old value (pre-edit value) in all cases when the data is invalid and not the first valid value as in scenario 3.

Cheers,


3 Answers, 1 is accepted

Sort by
0
Accepted
Jack
Telerik team
answered on 05 Mar 2013, 11:41 AM
Hi Mark,

Thank you for contacting us. This issue is addressed in our latest release - Q1 2013 and I recommend that you try this version. We will appreciate also your feedback regarding our new control - RadPdfViewer.

If you have further questions, do not hesitate to ask.
 
Regards,
Jack
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Join us for a FREE webinar to see all the new stuff in action.

Interested, but can’t attend? Register anyway and we’ll send you the recording.
0
Lalita P.
Top achievements
Rank 1
answered on 03 Sep 2014, 10:12 AM
Hi,

We also found the same problem (as scenario 3 above)
- we get the duplicate decimal point once we enter 1.2 on the highlighted decimal column (so it displays 1.2.00)
- if we enter .5 on the highlighted decimal column (without pressing F2 or double click that cell), it will show only 5

However, we have the constraint that we could not upgrade to release Q1 2013 (Now we are using Q2, 2012). what could be the alternative way to solve this problem?

Thanks in advance.
0
George
Telerik team
answered on 08 Sep 2014, 09:37 AM
Hi Lalita,

Thank you for writing.

In order to avoid the double decimal point behavior you should remove the characters after the decimal point when starting to edit a cell:
int previousDecimalPlaces;
void Grid_CellEditorInitialized(object sender, GridViewCellEventArgs e)
{
    var element = ((e.ActiveEditor as GridSpinEditor).EditorElement as GridSpinEditorElement);
    this.previousDecimalPlaces = element.DecimalPlaces;
    element.DecimalPlaces = 0;
    element.TextBoxItem.SelectionLength = 0;
    string text = element.TextBoxItem.Text;
    if (!string.IsNullOrEmpty(text))
    {
        int index = text.IndexOf(".");
        if (index != -1)
        {
            element.TextBoxItem.Text = text.Remove(index, 3);
        }
    }
 
    element.ValueChanged -= element_ValueChanged;
    element.ValueChanged += element_ValueChanged;
}
 
void element_ValueChanged(object sender, EventArgs e)
{
    (sender as GridSpinEditorElement).DecimalPlaces = this.previousDecimalPlaces;
}

In order to type the full stop char, you can use the KeyDown event:
void Grid_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyValue == 110) //'.'
    {
        this.Grid.BeginEdit();
        SendKeys.Send(".");
    }
}

I hope this helps.

Regards,
George
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Mark
Top achievements
Rank 1
Answers by
Jack
Telerik team
Lalita P.
Top achievements
Rank 1
George
Telerik team
Share this question
or