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

Force Uppercase for Cell Editing

2 Answers 101 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 10 Aug 2012, 10:06 PM
Hi,

I have a RadGridView set up in a way that's similar to the 'search as you type' demo, and my View-Model is handling things like the auto-generation of the columns in the grid.  What I'd like to do is this.  When a row is created or edited, any text that is entered into a particular column needs to be in all caps.  How can I do it?



Thanks,
Aaron

2 Answers, 1 is accepted

Sort by
0
Accepted
Maya
Telerik team
answered on 13 Aug 2012, 07:36 AM
Hello Aaron,

There are a couple of way to handle such case, but all of them require some custom implementation since TextBox (which is the default editing element does not support upper case out-of-the-box). What you can try is to handle PreparedCellForEdit event and find the TextBox:

void playersGrid_PreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
        {
            var edtingElement = e.EditingElement as TextBox;
            if (edtingElement != null)
            {              
            }
     }
 
Now you can work directly with the TextBox and implement the solution that sound most reasonable for you. Different approaches are suggested in those threads (link 1, link 2, link 3, link 4).

Kind regards,
Maya
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Aaron
Top achievements
Rank 1
answered on 14 Aug 2012, 04:20 PM
Thank you, Maya.

In case anyone is interested, this is the solution I went with.

1.    I created a TriggerAction class, strongly typed to TextBox, that calls the ToUpper() method on the Text property.

public class UppercaseAction : TriggerAction<TextBox>
    {
        protected override void Invoke(object parameter)
        {
            var selectionStart = AssociatedObject.SelectionStart;
            var selectionLength = AssociatedObject.SelectionLength;
            AssociatedObject.Text = AssociatedObject.Text.ToUpper();
            AssociatedObject.SelectionStart = selectionStart;
            AssociatedObject.SelectionLength = selectionLength;
        }
    }

2.    In my View-Model, I created an event handler for the RadGridView's PreparedCellForEdit event.

public void OnPreparedCellForEdit(object sender, GridViewPreparingCellForEditEventArgs e)
        {
            // Unless the cell is in the Code or Cause Code column, return:
            if (e.Column.UniqueName != CODE_COLUMN_UNIQUE_NAME &&
                e.Column.UniqueName != CAUSE_CODE_COLUMN_UNIQUE_NAME)
            {
                return;
            }
 
            var editingElement = e.EditingElement as TextBox;
            if (editingElement != null)
            {
                // Create an event trigger for a TextChanged event:
                System.Windows.Interactivity.EventTrigger eventTrigger = new System.Windows.Interactivity.EventTrigger("TextChanged");
 
                // Add an UppercaseAction to the event trigger:
                eventTrigger.Actions.Add(new UppercaseAction());
 
                // Add the event trigger to the cell's TextBox:
                Interaction.GetTriggers(editingElement).Add(eventTrigger);
            }
        }

3.    In the View, I bound the View-Model's event handler to the View using a CallMethodAction.

<telerik:RadGridView x:Name="radGridView1">
    <iny:Interaction.Triggers>
        <iny:EventTrigger EventName="PreparedCellForEdit">
            <ins:CallMethodAction MethodName="OnPreparedCellForEdit" TargetObject="{Binding}"/>
        </iny:EventTrigger>
    </iny:Interaction.Triggers>           
</telerik:RadGridView>

The XML namespaces:

The link I used was this one (number 3).
Tags
GridView
Asked by
Aaron
Top achievements
Rank 1
Answers by
Maya
Telerik team
Aaron
Top achievements
Rank 1
Share this question
or