Set focus of newly added row

4 posts, 0 answers
  1. Afsal
    Afsal avatar
    24 posts
    Member since:
    Dec 2010

    Posted 29 Dec 2011 Link to this post

    Hi,
    I am using radgrid and setting the ItemsSource to a collection. When a new item is added to this collection it will create a new empty row in the radgrid. But I am not able to set focus on this newly added row. I need to scroll down to find the newly added row. How can I set focus to the newly added row? I am using MVVM.
    Below are the code snippets,
    xaml -<telerikGrid:RadGridView ShowGroupPanel="False" ColumnWidth="*"

    Name="radSummaryLayout" AutoGenerateColumns="False" ItemsSource="{Binding CollectionName}" Height="Auto"

    ScrollMode="RealTime" AutoExpandGroups="False" CanUserDeleteRows="False" CanUserFreezeColumns="False" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" ShowColumnHeaders="True" BorderBrush="#FF7C87B1" BorderThickness="1" IsFilteringAllowed="False" AllowDrop="False">

    In ViewModel- the following code executes in a button click event.
    Dim eMem As New Item
    CollectionName.Add(eMem)

    Thanks in advance,
    Afsal
  2. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 30 Dec 2011 Link to this post

    Hello Afsal,

    You could scroll to the newly added row, using a method which is called ScrollIntoViewAsync(). It takes an action as parameter and this action will be performed when all requested scroll operations are completed. You can take a look at the following snippet for a reference how to use this method in your scenario:

    grid.ScrollIntoViewAsync(row,  column,
                        (r) =>
                        {
                            GridViewRow row = r as GridViewRow;
                            if(row != null)
                            {
                                  row.Cells[0].Focus();
                                  cell.IsSelected = true;
                                  cell.IsCurrent = true;
                                  cell.ParentRow.GridViewDataControl.CurrentColumn = cell.Column;
                                  cell.ParentRow.GridViewDataControl.CurrentItem = cell.DataContext;
                            }
                        });

    Let me know if this doesn't help.

    Kind regards,
    Didie
    the Telerik team

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

  3. Afsal
    Afsal avatar
    24 posts
    Member since:
    Dec 2010

    Posted 30 Dec 2011 Link to this post

    Hi Didie,

    Thanks for your reply. I tried that but not worked. Since i am adding the collection in viewmode I tried to call the ScrollIntoViewAsync in RowLoaded event. I tried the below code, 

    grd.ScrollIntoViewAsync(e.Row,

     

    Function(f)

    Dim row As GridViewRow = TryCast(e.Row, GridViewRow)

    If row IsNot Nothing Then

    Dim cell As GridViewCell = row.Cells(0)

    row.Cells(0).Focus()

    cell.IsSelected = True

     

    cell.IsCurrent =

    True

     

    cell.ParentRow.GridViewDataControl.CurrentColumn = cell.Column

    cell.ParentRow.GridViewDataControl.CurrentItem = cell.DataContext

     

                                                                  

    End If

    End Function)


    Am I missing something? Can you give me a sample application?
    Many thanks,
    Afsal

     

     
  4. Dimitrina
    Admin
    Dimitrina avatar
    3769 posts

    Posted 30 Dec 2011 Link to this post

    Hi Afsal,

     I would need more information in order to find out why this code is not working at your end. Could you please tell me how is your ItemsSource defined? When do you add a new item - on a button click maybe?

    Regards,
    Didie
    the Telerik team

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

Back to Top