FilterRow Focus

9 posts, 1 answers
  1. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 06 Nov 2012 Link to this post

    Hello!

    One simple question: How can I set the focus to the FilterRow within a RadGridView?
    Thanks a lot!!!
    Tim.
  2. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2477 posts

    Posted 06 Nov 2012 Link to this post

    Hello,

    Here is one possible approach to do this:

    public MainWindow()
    {
        InitializeComponent();
     
        this.clubsGrid.Loaded += new RoutedEventHandler(clubsGrid_Loaded);
    }
     
    void clubsGrid_Loaded(object sender, RoutedEventArgs e)
    {
        var grid = ((RadGridView)sender);
     
        var firstHeaderCell = grid.ChildrenOfType<GridViewHeaderCell>()
            .SingleOrDefault(headerCell => headerCell.Column == grid.Columns["Name"]);
             
        var firstFilterControl = firstHeaderCell.ChildrenOfType<FieldFilterControl>().FirstOrDefault();
        if (firstFilterControl != null)
        {
            // You may want to change this if the first column shows a RadDateTimePicker instead of StringFilterEditor.
            var textBox = firstFilterControl.ChildrenOfType<TextBox>().FirstOrDefault();
            if (textBox != null)
            {
                textBox.Focus();
            }
        }
    }

    I have attached a sample project which you can tweak to suit your needs.

    All the best,
    Rossen Hristov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 06 Nov 2012 Link to this post

    Hi Rossen, thanks a lot for the fast reply! I'll try it...
  4. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 06 Nov 2012 Link to this post

    Hello. Unfortunately with Silverlight and

    var firstHeaderCell = grid.ChildrenOfType<GridViewHeaderCell>().SingleOrDefault(headerCell => headerCell.Column == grid.Columns["Label"]);

    firstHeaderCell is always null! My column definition looks something like:

    <telerik:GridViewDataColumn UniqueName="Label" Header="Labels:" Width="*">
        <telerik:GridViewDataColumn.CellTemplate>
            <DataTemplate>
                <telerik:RadButton HorizontalContentAlignment="Left">
                    <TextBlock Text="{Binding Label}" />
                </telerik:RadButton>
            </DataTemplate>
        </telerik:GridViewDataColumn.CellTemplate
    </telerik:GridViewDataColumn>

    I tried it with different LINQ queries but all I get are exceptions... ;) Any ideas?
    Thanks a lot,
    Tim.




  5. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2477 posts

    Posted 06 Nov 2012 Link to this post

    Hello,

    1. Can you try on a different event such as DataLoaded?

    2. Does grid.ChildrenOfType<GridViewHeaderCell>() return any header cells at all?

    Kind regards,
    Rossen Hristov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  6. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 06 Nov 2012 Link to this post

    Hi Rossen,

    the code is working for some events, for example FilterOperatorsLoading, but the focus will not be set anyway - perhaps because the control is not yet being displayed? It works "perfectly" with the MouseMove event - but of course nobody want to do that!!! Unfortunately I didn't found an event that suits my needs! Ideas? Thanks a lot!!!

    Tim.
  7. Answer
    Rossen Hristov
    Admin
    Rossen Hristov avatar
    2477 posts

    Posted 07 Nov 2012 Link to this post

    Hello,

    Silverlight is a tricky platform regarding the focus. Here is what I invented:

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
     
            this.clubsGrid.LayoutUpdated += clubsGrid_LayoutUpdated;
        }
     
        void clubsGrid_LayoutUpdated(object sender, EventArgs e)
        {
            if (firstEditor != null)
            {
                System.Windows.Browser.HtmlPage.Plugin.Focus();
                 
                firstEditor.ChildrenOfType<TextBox>().Single().Focus();
                 
                this.clubsGrid.LayoutUpdated -= clubsGrid_LayoutUpdated;
            }
        }
     
        private FrameworkElement firstEditor;
         
        private void clubsGrid_FieldFilterEditorCreated_1(object sender, Telerik.Windows.Controls.GridView.EditorCreatedEventArgs e)
        {
            if(e.Column == this.clubsGrid.Columns["Name"])
            {
                this.firstEditor = e.Editor;
            }
        }

    You can find a working project attached.

    Greetings,
    Rossen Hristov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  8. Tim
    Tim avatar
    114 posts
    Member since:
    Aug 2011

    Posted 07 Nov 2012 Link to this post

    Hi Rossen,

    thank you so much for your help!!! And I have to say: That did the trick!!! Well, not to 100%; I ended up with something like:
    TextBox tb = this._firstEditor.ChildrenOfType<TextBox>().Single();
                tb.GotFocus -= this.TbGotFocus;
                tb.GotFocus += this.TbGotFocus;
                tb.Focus();
            }
     
            void TbGotFocus(object sender, RoutedEventArgs e)
            {
                this.RadGridView1.LayoutUpdated -= RadGridView1LayoutUpdated;
            }

    Only with this change I can be 100% sure that the focus is set!
    Thanks a lot, Rossen!!!
    Tim.

  9. Rossen Hristov
    Admin
    Rossen Hristov avatar
    2477 posts

    Posted 07 Nov 2012 Link to this post

    Hello,

    Just have in mind that if your column is DateTime this will be a RadDateTimePicker instead of a TextBox.

    Boolean columns are also different.

    Greetings,
    Rossen Hristov
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top