How to set focus on given cell?

21 posts, 0 answers
  1. Virendra
    Virendra avatar
    50 posts
    Member since:
    Aug 2012

    Posted 11 May 2009 Link to this post

    Hi,

    i want to set focus to a given cell, when grid loaded 
    and afterward programaticly on click of buttun.

    Thanks
    virendra
  2. Stefan Dobrev
    Admin
    Stefan Dobrev avatar
    790 posts

    Posted 12 May 2009 Link to this post

    Hello Virendra,

    I have prepared a sample solution that shows how to achieve these tasks. Please find it in the attached file. Hope you will find it useful.

    Sincerely yours,
    Stefan Dobrev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Virendra
    Virendra avatar
    50 posts
    Member since:
    Aug 2012

    Posted 12 May 2009 Link to this post

    Thanks Stefan,

    what will happen in you example when more then one cells are having same value, it will set focus to first one.

    How will you set focus to a cell in..say...RowIndex is 1 and ColumnIndex is 2?

    Thanks
    virendra
  4. Stefan Dobrev
    Admin
    Stefan Dobrev avatar
    790 posts

    Posted 14 May 2009 Link to this post

    Hello Virendra,

    You are right. If there are more than one cell with the given value the first will be selected.

    I have prepared a sample with RadNumericUpDown, which shows how to select cell by row and column. Hope you will find it useful.

    Greetings,
    Stefan Dobrev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  5. Thomas
    Thomas avatar
    5 posts
    Member since:
    Jun 2009

    Posted 02 Jun 2009 Link to this post

    Hi Stefan

    thanks for this sample, works fine!
    I like to put this code into a class to have the code unique for all RadGridView's in my project.
    I tried to pass the RadGridView-Object as a parameter to the GetCellByIndecies - function. But by this way I don't have the possibility to get the cells by ChildrenOfType. Also I only have a GridView.Columns - Object, but no GridView.Rows - Object. Any chance to handle this?

    Thanks for your help
    Thomas
  6. Stefan Dobrev
    Admin
    Stefan Dobrev avatar
    790 posts

    Posted 02 Jun 2009 Link to this post

    Hi Thomas,

    Here are some handy extension methods that you can reuse in your code:
    public static class GridViewExtensions 
        public static GridViewCell GetCellByContent(this RadGridView gridView, object cellValue) 
        { 
            return 
                (from cell in gridView.ChildrenOfType<GridViewCell>() 
                 where cell.Value.ToString() == cellValue.ToString() 
                 select cell).FirstOrDefault(); 
        } 
     
        public static GridViewCell GetCellByIndexes(this RadGridView gridView, int rowIndex, int columnIndex) 
        { 
            return 
                (from cell in gridView.ChildrenOfType<GridViewCell>() 
                 where gridView.Columns.IndexOf(cell.Column) == columnIndex 
                 select cell).Skip(rowIndex).FirstOrDefault(); 
     
        } 

    You should use these methods after the GridView's visual tree is constructed. Also you note that ChildrenOfType<>() extension method is in Telerik.Windows.Controls namespace.

    Hope this helps.

    Greetings,
    Stefan Dobrev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  7. Thomas
    Thomas avatar
    5 posts
    Member since:
    Jun 2009

    Posted 02 Jun 2009 Link to this post

    perfect, thank you!
  8. amit
    amit avatar
    21 posts
    Member since:
    Nov 2007

    Posted 04 May 2010 Link to this post

    Hi

    I have tried following code to select Cell in RadControls for Silverlight Q1 2010 SP1. But it didn't work. It doesn't select Cell or Row.

            private void SetCurrentCellByIndecies(int rowIndex, int columnIndex)
            {
                GridViewCell cell = this.GetCellByIndecies(rowIndex, columnIndex);
                SetCurrentCell(cell);
            }
            private GridViewCell GetCellByIndecies(int rowIndex, int columnIndex)
            {
                return
                    (from cell in this.uxGrid.ChildrenOfType<GridViewCell>()
                     where uxGrid.Columns.IndexOf(cell.Column) == columnIndex
                     select cell).Skip(rowIndex).FirstOrDefault();
            }

    Am I missing anything?

    Thank you
  9. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 10 May 2010 Link to this post

    Hi amit,

    Are you trying to just set the cell as current , or you need to set the cell current , enter edit mode and set the keyboard focus in it ?

    Regards,
    Pavel Pavlov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  10. amit
    amit avatar
    21 posts
    Member since:
    Nov 2007

    Posted 10 May 2010 Link to this post

    Hi Pavel, thanks for reply

    I need to set the cell current and set the keyboard focus in it (like grid does when I click on cell). Enter in edit mode is optional.

    Thank you.
  11. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 13 May 2010 Link to this post

    Hi amit,

    I have updated the sample posted previously at this thread to support the new binaries.
    Please find it attached.

    Greetings,
    Pavel Pavlov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  12. amit
    amit avatar
    21 posts
    Member since:
    Nov 2007

    Posted 13 May 2010 Link to this post

    Hi Pavel,

    Thanks for reply and putting sample together. Unfortunately, it didn't work for me all the times. I think it's because of Virtualization. Because I have Virtualization on and I couldn't select cell outside of viewable area. It worked for cell within viewable area. Do you think Virtualization can break the code you sent me? If yes, how to fix it?

    Thank you,
    Amit
  13. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 14 May 2010 Link to this post

    Hello amit,

    You may need to call the RadGridVIew.ScrollIntoView method , before setting the current cell.  This way you will be sure the relevant cell is in the viewport  before trying to make it current.

    Let me know in case this does not help.

    Regards,
    Pavel Pavlov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  14. amit
    amit avatar
    21 posts
    Member since:
    Nov 2007

    Posted 14 May 2010 Link to this post

    Hi Pavel,

    Thanks for reply. I would try it . But ScrollIntoView method doesn't scroll content for me when row and column visualization is on.

    Thank you,
    Amit
  15. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 17 May 2010 Link to this post

    Hi amit,

    If you use virtualization , this means that you do not have all the cells available, until you scroll.

    Maybe in your case  a more suitable approach would be to set the current cell passing a data item and column , rather than passing row and column index ?

    Let me know if such approach is applicable  so  I can prepare a sample for you

    Regards,
    Pavel Pavlov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  16. amit
    amit avatar
    21 posts
    Member since:
    Nov 2007

    Posted 25 May 2010 Link to this post

    Yes, I can give it a shot. Can you please prepare an example?

    Thank you,
    Amit
  17. Nicolas
    Nicolas avatar
    3 posts
    Member since:
    Nov 2010

    Posted 12 Jan 2011 Link to this post

    Hello,
    Good example, but the cell is not in edit mode. How to do this ?
    Thank you.
  18. Pavel Pavlov
    Admin
    Pavel Pavlov avatar
    2039 posts

    Posted 12 Jan 2011 Link to this post

    Hi Nicolas ,
    Have you tried  calling the BeginEdit() method of RadGridView ?  This should force the current cell to enter edit mode.

    All the best,
    Pavel Pavlov
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  19. Nicolas
    Nicolas avatar
    3 posts
    Member since:
    Nov 2010

    Posted 12 Jan 2011 Link to this post

    Thank you.
    I used:

    cell.IsInEditMode =

     

    true;

     

  20. Dorothee
    Dorothee avatar
    2 posts
    Member since:
    Mar 2011

    Posted 24 Mar 2011 Link to this post

    Hello!

    I am inserting a new row (or rather a new item in the itemssource) and then set the focus on a cell in this row, so that you can edit the cell, like in the example code. It works fine.
    But when the column is sorted, the new row is inserted at the correct position, but sometimes, the cell ist losing the focus. This behaviour occurs, when the new row is inserted into a region, which ist not visible before inserting.

    How can I sove this problem?

    Thanks for help
  21. Alden
    Alden avatar
    2 posts
    Member since:
    Sep 2012

    Posted 26 Jun 2013 Link to this post

    Good idea with the extension methods.

    Here is one I came up with after reading this post:

    public static class Extensions

     

     

    {

     

    public static GridViewCell GetCellByCellInfo(this RadGridView gridView, GridViewCellInfo cellInfo)

     

     

    {

     

    GridViewRow RowFound = gridView.ChildrenOfType<GridViewRow>().Where(w => w.Item == cellInfo.Item).FirstOrDefault();

    GridViewCellBase cellFound = RowFound.Cells.Where(c => c.Column == cellInfo.Column).FirstOrDefault();

    return cellFound as GridViewCell;

     

     

    }
    }

Back to Top