Scroll to row without selection

4 posts, 0 answers
  1. Ken
    Ken avatar
    10 posts
    Member since:
    Sep 2011

    Posted 03 Feb 2012 Link to this post

    In my case I want to scroll to the top row in certain circumstances.  The code that works isn't elegant as it will fire events that I really don't want.  To get around that I use a flag that I set before updating the grid, then reset after.  Here's the code:

                _fillingSearchGrid = true;
                SearchResultsGrid.DataSource = results;
                if (SearchResultsGrid.Rows.Count > 0)
                {
                    SearchResultsGrid.CurrentRow = SearchResultsGrid.ChildRows[0];
                    SearchResultsGrid.CurrentRow = null;
                }
                _fillingSearchGrid = false;

    Is there a better way to do this?
  2. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 08 Feb 2012 Link to this post

    Hello Ken,

    Thank you for writing.

    If you will always be scrolling to the first row, you can simply move the scroll bar. Here is the code to scroll to the first row:
    this.radGridView1.TableElement.VScrollBar.Value = 0;

    You can also scroll to a specific row and/or column without changing the current selection by using the RadGridView.TableElement ScrollTo, ScrollToRow and ScrollToColumn methods.

    I hope this will be useful for you. Should you have further questions, I would be glad to help.

    All the best,
    Ivan Petrov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. Ken
    Ken avatar
    10 posts
    Member since:
    Sep 2011

    Posted 13 Feb 2012 Link to this post

    Thanks, that works.  Now...is there any way to set a datasource without the first row in the grid being automatically selected?
  4. Ivan Petrov
    Admin
    Ivan Petrov avatar
    705 posts

    Posted 16 Feb 2012 Link to this post

    Hi Ken,

    Thank you for your reply.

    You can use the CurrentRowChanging event to cancel the selection when you are binding the RadGridView.

    Here is a code example of the above:
    bool cancelSelection = false;
     
    this.radGridView1.CurrentRowChanging += new CurrentRowChangingEventHandler(radGridView1_CurrentRowChanging);
    this.cancelSelection = true;
    this.radGridView1.DataSource = someSource;
    this.cancelSelection = false;
     
    private void radGridView1_CurrentRowChanging(object sender, CurrentRowChangingEventArgs e)
    {
      e.Cancel = this.cancelSelection;
    }

    I hope this will help. If you need further assistance feel free to write back.

    All the best,
    Ivan Petrov
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Back to Top