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

Programmatic row selection

3 Answers 219 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris Kirkman
Top achievements
Rank 1
Chris Kirkman asked on 01 Jun 2011, 09:43 PM
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.

3 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 05 Jun 2011, 09:35 AM
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
0
Accepted
Alexander
Telerik team
answered on 07 Jun 2011, 10:13 AM
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.
0
Chris Kirkman
Top achievements
Rank 1
answered on 07 Jun 2011, 03:42 PM
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.
Tags
GridView
Asked by
Chris Kirkman
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Alexander
Telerik team
Chris Kirkman
Top achievements
Rank 1
Share this question
or