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

Support for Carriage return in grid cell

2 Answers 158 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Dominik
Top achievements
Rank 1
Dominik asked on 14 Sep 2011, 01:25 PM
Hi

We need the grid to be able to support the carriage return, so that the user can enter a text with multiple lines in a grid cell.
Currently, this does not work. Upon pressing enter, the focus will be set on the cell below.

Accordung to the following thread:
http://www.telerik.com/community/forums/silverlight/gridview/carriage-return-support-in-grid-cell.aspx

this should work natively. However, the thread is old.

Is there something I must do, to get this to work?

Thanks in advance

2 Answers, 1 is accepted

Sort by
0
Dominik
Top achievements
Rank 1
answered on 15 Sep 2011, 03:04 PM
Hi

I found a solution that works well. However, from the thread i have seen i thought this would be supported nativly.

Here is my code:

public class CustomDataColum : GridViewDataColumn
  {
      public CustomDataColum(): base()
      {
        
      }
 
 
      #region multiline support
 
      public int CellHeightForMultiline
      {
          get;
          set;
      }
 
      public bool PreventMultiLine { get; set; }
 
      public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
      {
          if (this.DataType != typeof(string) || PreventMultiLine)
          {
              return base.CreateCellEditElement(cell, dataItem);
          }
          var editorTextBox = new TextBox();
 
          Binding valueBinding = CreateValueBinding(this.DataMemberBinding, true);
 
          this.BindingTarget = TextBox.TextProperty;
          editorTextBox.SetBinding(TextBox.TextProperty, valueBinding);
 
          editorTextBox.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
          editorTextBox.AcceptsReturn = true;
          editorTextBox.MinHeight = this.CellHeightForMultiline;
          editorTextBox.VerticalAlignment = VerticalAlignment.Stretch;
          editorTextBox.HorizontalAlignment = HorizontalAlignment.Stretch;
          editorTextBox.Loaded += EditorTextBoxLoaded;
 
          return editorTextBox;
      }
 
      private static void EditorTextBoxLoaded(object sender, RoutedEventArgs e)
      {
          var editorTextBox = sender as TextBox;
          if (editorTextBox != null)
          {
              editorTextBox.SelectAll();
              editorTextBox.Focus();
          }
      }
 
      private static Binding CreateValueBinding(Binding sourceBinding, bool isEditable)
      {
          var valueBinding = new Binding();
          valueBinding.Path = new PropertyPath(sourceBinding.Path.Path);
          if (isEditable)
          {
              valueBinding.Mode = BindingMode.TwoWay;
          }
          else
          {
              valueBinding.Mode = BindingMode.OneWay;
          }
          valueBinding.NotifyOnValidationError = true;
          valueBinding.ValidatesOnExceptions = true;
          valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
          return valueBinding;
      }
 
      #endregion

Maybe there is another (better?) way but this works surprisingly well.

Greetings

0
Maya
Telerik team
answered on 17 Sep 2011, 07:41 AM
Hello Dominik,

Indeed, this is not supported for the time being. The way to go is to set AcceptReturns property of the editing element (TextBox) - just as you have already done. 

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Dominik
Top achievements
Rank 1
Answers by
Dominik
Top achievements
Rank 1
Maya
Telerik team
Share this question
or