How to set focus to textbox in edit of Raddatagrid

1 Answer 114 Views
DataGrid
Satya
Top achievements
Rank 1
Satya asked on 23 Mar 2023, 06:48 AM
How to set focus to textbox in edit of Raddatagrid in Tappped command

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 27 Mar 2023, 08:43 PM

Hello Satya,

I can't really tell what you are asking for because the question is a bit contradictory. A single tap has no editor to put focus on, are you trying to put the cell into EditMode in a single tap? The cell will automatically get focus when it's placed in edit mode, so what it sounds like what you really want is a one-tap edit option.

In any case, the DataGridCommand base class has an Owner property, which is a direct reference to the DataGrid that is being used! This means you can do nearly anything you want from inside the command.

Next, visit the following documentation UWP DataGrid Documentation - Edit Operation, and then please read Edit Operation - Programmatic Editing. You can programmatically invoke editing, which will automatically put the focus in the editor.

Now, knowing both of the items I shared above, you should now know you can invoke the BeginEdit command for that cell that was tapped:

public class CustomCellTapCommand : DataGridCommand
{
    public CustomCellTapCommand()
    {
        this.Id = CommandId.CellTap;
    }

    public override void Execute(object parameter)
    {
        var context = parameter as DataGridCellInfo;

        //  Invoke the BeginEdit command
        this.Owner.CommandService.ExecuteDefaultCommand(CommandId.BeginEdit, context);

        // whatever else you want to do can go here.
    }
}

For more information, please visit the following documentation articles as they have the rest of the information you're looking for:

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Satya
Top achievements
Rank 1
commented on 31 Mar 2023, 12:34 PM

Hi Lance,

Thanks for your reply. My requirement is when user clicks on Edit button / Taps on a row , user can edit 2 columns (Numeric column for Amount & Text column for Remarks ). Edit functionality was working fine but my issue was not able to set Focus to Amount column ( Default focus not working ) , so I wish to set Focus to Amount Numeric textbox programmatically by calling  Focus method but unfortunately I was not able to find the Numeric textbox.

While searching the articles 'Accessing controls in RadGrid' , i found article for RadGrid for ASP.Net Ajax. In this controls are able to find in ItemDataBound event. But that event not available for RadDataGrid for UWP

 

protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) { if (e.Item.IsInEditMode) { // Controls that are only available in Edit Mode GridEditableItem editItem = (GridEditableItem)e.Item; RadNumericTextBox rNumericTextBox = editItem["NumericCol"].Controls[0] as RadNumericTextBox;

}

}

 

Lance | Manager Technical Support
Telerik team
commented on 31 Mar 2023, 06:42 PM

Hi Satya,

Correct, there is no such event for UWP. I'm afraid I cannot offer an alternative solution through official APIs.

I can possibly give you a hint to move forward in building something custom.

Why not just define your own custom CellEditTemplate for that column and use your own control in there. This will allow you to have the final say in what you want to happen, for example, in the conrol's Loaded event handler

control class

public class MyNumericControl : RadNumericBox
{
    public MyNumericcontrol()
    {
        this.Loaded += MyLoadedEventHandler;
    }
    
    private async void MyLoadedEventHandler(..., ...)
    {
        await Task.Delay(500); // wait a short time to let this control steal the focus from something else
        this.Focus();
    }
}

use:

<CellEditTemplate>
    <DataTemplate>
        <MyNumericControl Value="{Binding MyValueProperty, Mode=TwoWay}"/>
    </DataTemplate>
</CellEditTemplate>

I hope this helps!

Tags
DataGrid
Asked by
Satya
Top achievements
Rank 1
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or