Incorrect CurrentRow selected after setting the DataSource in a sorted gridview

4 posts, 0 answers
  1. Victor
    Victor avatar
    2 posts
    Member since:
    Sep 2015

    Posted 06 Jan Link to this post

    When the datasource is set for a gridview with sorting applied, the first row in the Rows collection is selected rather than the first row in the ChildRows collection. This causes the gridview to scroll to the position of the first row in the datasource rather than the first displayed row in the gridview.

     

     Steps to reproduce:

    Set the datasource for the gridview

    Sort the gridview

    Set the datasource for the gridview again

    The first row in the datasource will be selected and scrolled to, rather than the first displayed row.

  2. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 07 Jan Link to this post

    Hello Victor,

    Thank you for writing.

    When you reset the RadGridView.DataSource in a sorted grid, it is necessary to set the CurrentRow to the desired value. Otherwise, you will obtain the last selected row and the grid will scroll to this row: 
    private void radButton1_Click(object sender, EventArgs e)
    {
        this.radGridView1.DataSource = null;
        this.radGridView1.DataSource = this.productsBindingSource;
        this.radGridView1.CurrentRow = this.radGridView1.ChildRows[0];
    }

    I hope this information helps. Should you have further questions I would be glad to help.
     
    Regards,
    Dess
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Victor
    Victor avatar
    2 posts
    Member since:
    Sep 2015

    Posted 07 Jan in reply to Dess Link to this post

    Is it possible to prevent redundant event firing using this method?
  5. Dess
    Admin
    Dess avatar
    1601 posts

    Posted 08 Jan Link to this post

    Hello Victor,

    Thank you for writing back. 

    I suppose that you want to prevent the CurrentRowChanged event from firing when using the provided code snippet from my previous post. In order to achieve it, you should unsubscribe from the event before setting the CurrentRow property and subscribe to it again afterwards:
    this.radGridView1.CurrentRowChanged -= radGridView1_CurrentRowChanged;
    this.radGridView1.CurrentRow = this.radGridView1.ChildRows[0];
    this.radGridView1.CurrentRowChanged += radGridView1_CurrentRowChanged;

    If it is not the exact event, please specify in details what redundant event firing you want to prevent.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
    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 Feedback Portal and vote to affect the priority of the items
Back to Top