Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
805
XamDataGrid Character length Validation
posted

Hi,
   In a xamDataGrid, I am trying to restrict the entry of a numeric value in a cell to the length of 10. I am not doing this with ValueConstraint wherein it displays a message 'invalid data' but still allows user to enter more than 10 characters. Instead I want the user to restrict typing more than 10 characters. To do that I use the keydown event, and the following code:-

if (grdData.ActiveCell.Field.Name == "NumericCellName")
     {
      CellValuePresenter cvp = CellValuePresenter.FromCell(grdData.ActiveCell);


      if (cvp != null)
      {
       if (cvp.Editor != null)
       {
        if (Convert.ToString(cvp.Editor.Value).Length > 10)
        {
         e.Handled = true;
         return;
        }

       }
      }
     }

It works fine when the user key-in the data, say after 10th character keyed in, it does not allow 11th character to be keyed in. But when the whole text is selected in the cell, and when the user types something still it restricts input which is wrong, when the whole text is selected, and some value is typed, it should ideally erase the selected text and accept the new data keyed in.

Please advice what had to be done towards this.

Thanks.