Programmatic row selection

4 posts, 1 answers
  1. Chris Kirkman
    Chris Kirkman avatar
    37 posts
    Member since:
    Jul 2012

    Posted 01 Jun 2011 Link to this post

    Anyone know how to do this?

    1.  My user has one to many rows selected
    2.  He performs an action, resulting in me re-binding the grid.
    3.  I'd like the items that were selected to be selected again after re-setting the datasource.

    Thanks.
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 05 Jun 2011 Link to this post

    Hello Chris,

    There is no clear way to do this, because of the following:
    You will not have the same instances of the objects binded to the grid and of course you will not have the same rows in the grid, but if you know the type of the objects in the grid, and if they have, let's say an identity column, then:
    1. Before rebinding, save the selected data bound objects to a temporary list
    2. Rebind the grid (having registered to the DataBindingComplete event)
    3. OnDataBindingComplete if you have a temporary selected list, just parse the grid rows, and if the databound object is the same (based on a condition of course) just select that row.

    Hope this is clear enough.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 07 Jun 2011 Link to this post

    Hello Chris,

    Your scenario depends on the new DataSource you set. If it is different that the previous one, you should use a solution, simillar to the one suggested by Emanuel.

    If you reset the DataSource with the same one, the following approach could be used:
    GridViewRowInfo[] selectedRows = this.radGridView1.SelectedRows.ToArray<GridViewRowInfo>();
     
    this.radGridView1.DataSource = null;
    this.radGridView1.DataSource = this.dataSource;
     
    foreach (GridViewRowInfo selectedRow in selectedRows)
    {
        foreach (GridViewRowInfo row in this.radGridView1.Rows)
        {
            if (row.DataBoundItem == selectedRow.DataBoundItem)
            {
                row.IsSelected = true;
            }
        }
    }

    I hope it helps.

    Best regards,
    Alexander
    the Telerik team
    Q1’11 SP1 of RadControls for WinForms is available for download; also available is the Q2'11 Roadmap for Telerik Windows Forms controls.
  5. Chris Kirkman
    Chris Kirkman avatar
    37 posts
    Member since:
    Jul 2012

    Posted 07 Jun 2011 Link to this post

    Yes, in my case the datasource remained the same; although some properties of members within my datasource did change (this the need for refreshing the datasource).  Anyway, I was able to store the unique ID of each member of my datasource in an array, refresh the datasource and then use a for loop to set the visible property of each item in the grid to true immediately after setting the datasource.
Back to Top