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

How to set focus on given cell?

20 Answers 774 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Virendra
Top achievements
Rank 1
Virendra asked on 11 May 2009, 07:39 AM
Hi,

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

Thanks
virendra

20 Answers, 1 is accepted

Sort by
0
Stefan Dobrev
Telerik team
answered on 12 May 2009, 08:42 AM
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.
0
Virendra
Top achievements
Rank 1
answered on 12 May 2009, 09:22 AM
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
0
Stefan Dobrev
Telerik team
answered on 14 May 2009, 12:20 PM
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.
0
Thomas
Top achievements
Rank 1
answered on 02 Jun 2009, 07:29 AM
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
0
Stefan Dobrev
Telerik team
answered on 02 Jun 2009, 08:36 AM
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.
0
Thomas
Top achievements
Rank 1
answered on 02 Jun 2009, 09:37 AM
perfect, thank you!
0
amit
Top achievements
Rank 1
answered on 04 May 2010, 07:40 PM
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
0
Pavel Pavlov
Telerik team
answered on 10 May 2010, 12:38 PM
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.
0
amit
Top achievements
Rank 1
answered on 10 May 2010, 07:46 PM
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.
0
Pavel Pavlov
Telerik team
answered on 13 May 2010, 02:23 PM
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.
0
amit
Top achievements
Rank 1
answered on 13 May 2010, 10:31 PM
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
0
Pavel Pavlov
Telerik team
answered on 14 May 2010, 01:59 PM
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.
0
amit
Top achievements
Rank 1
answered on 14 May 2010, 05:50 PM
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
0
Pavel Pavlov
Telerik team
answered on 17 May 2010, 03:05 PM
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.
0
amit
Top achievements
Rank 1
answered on 25 May 2010, 03:43 PM
Yes, I can give it a shot. Can you please prepare an example?

Thank you,
Amit
0
Nicolas
Top achievements
Rank 1
answered on 12 Jan 2011, 05:28 PM
Hello,
Good example, but the cell is not in edit mode. How to do this ?
Thank you.
0
Pavel Pavlov
Telerik team
answered on 12 Jan 2011, 06:33 PM
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>>
0
Nicolas
Top achievements
Rank 1
answered on 12 Jan 2011, 06:34 PM
Thank you.
I used:

cell.IsInEditMode =

 

true;

 

0
Dorothee
Top achievements
Rank 1
answered on 24 Mar 2011, 10:30 AM
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
0
Alden
Top achievements
Rank 1
answered on 26 Jun 2013, 08:43 PM

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;

 

 

}
}

Tags
GridView
Asked by
Virendra
Top achievements
Rank 1
Answers by
Stefan Dobrev
Telerik team
Virendra
Top achievements
Rank 1
Thomas
Top achievements
Rank 1
amit
Top achievements
Rank 1
Pavel Pavlov
Telerik team
Nicolas
Top achievements
Rank 1
Dorothee
Top achievements
Rank 1
Alden
Top achievements
Rank 1
Share this question
or