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

GridViewRows.IsSelected

2 Answers 111 Views
GridView
This is a migrated thread and some comments may be shown as answers.
David Silver
Top achievements
Rank 1
David Silver asked on 10 Aug 2009, 09:21 AM
Hi,
I am trying to Select the very first row in my DataGrid. I used a couple of techniques to achieve it. The following one works fine where I explicity specify the contents of a cell.

var row = PeopleDataGrid.ChildrenOfType<GridViewRow>().Where(r => r.ChildrenOfType<GridViewCell>().Where(c => c.Content.ToString() == "David").Any()).First();  
                if (row != null)  
                {  
                    row.IsSelected = true;  
                } 

However I thought that following should work on the same lines but it doesn't.
var row = PeopleDataGrid.ChildrenOfType<GridViewRow>().First();  
                if (row != null)  
                {  
                    row.IsSelected = true;  
                } 

The Grid is bound to an XML file with multiple Employee records and I noticed that in the latter approach , following property remains null.
((Telerik.Windows.Controls.GridView.DetailsProvider)row.DetailsProvider).DataContext 

Please guide how can I achieve Selection of first row of DataGrid without specifying cell content. Also could you tell why doesn't the latter approach work when it retrieves the Row alright from ChildrenOfType<GridViewRow> collection? Thanks.
 
Best Regards,
David

2 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 12 Aug 2009, 11:29 AM
Hi David Silver,

To select the first row please use this:

GridViewRow gridViewRow = this.playersGrid.ItemsControl.ItemsGenerator.GenerateItemAtIndex(0) as GridViewRow;

This will give you the first row. Please see the project I have attached for a demo.

Where are you using the DetailsProvider and what for? You should not need it in general as it is the link between rows and their row details, but it is not supposed to be used directly from your code. Do you have row details? If you need the DataContext of a row -- simply use row.DataContext.

Can you elaborate on what are you trying to achieve and, if possible, send us a sample project and we will help you.

Greetings,
Ross
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
David Silver
Top achievements
Rank 1
answered on 13 Aug 2009, 11:57 AM
Hello Ross,

Thankyou for your prompt reply and giving a solution about Row Selection. I have rows with details but I am not directly using DetailsProvider, since the following code

var row = PeopleDataGrid.ChildrenOfType<GridViewRow>().First();  
                if (row != null)  
                {  
                    row.IsSelected = true;  
                } 

was not working I just thought that may be "row.DetailsProvider.DataContext " was the cause. Anyways, thanks again for solving the problem at hand.

Best Regards,
David
Tags
GridView
Asked by
David Silver
Top achievements
Rank 1
Answers by
Rossen Hristov
Telerik team
David Silver
Top achievements
Rank 1
Share this question
or