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

[Solved] Count property of GridView after setting ItemSource

3 Answers 71 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Todd Millett
Top achievements
Rank 1
Todd Millett asked on 11 Oct 2010, 09:10 AM

i am using the following code to set the itemsource property to a GridView to a collection. But after setting the itemsource, when i try to access the items[0], I get an exception that there are no elements, event though the itemssource has some elements.
--------------------------------------------------------------------------

this

 

.dgiEmail.ItemsSource = value;

 

 

this.dgEmail.Rebind();

 

 

if (ViewModel.SelectedEmailResults.Count == 0)

 

{

 

if (this.emailItemList1.Count > 0)

 

{

 

this.dgEmail.SelectedItem = this.dgEmail.Items[0];

 

}

}

3 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 11 Oct 2010, 09:14 AM
Hi,

 Can you post more info about this collection?

All the best,
Vlad
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Todd Millett
Top achievements
Rank 1
answered on 11 Oct 2010, 09:17 AM
Its an observable collection which is getting returned from a WCF service. I have a property which sets the itemsource :

public

 

ObservableCollection<ExchangeResultItem> EmailResults1

 

{

 

get { return this.emailItemList1; }

 

 

set

 

{

 

this.emailItemList1 = value;

 

 

this.dgEmail.ItemsSource = value;

 

 

this.dgEmail.Rebind();

 

 

if (ViewModel.SelectedEmailResults.Count == 0)

 

{

 

if (this.emailItemList1.Count > 0)

 

{

 

this.dgEmail.SelectedItem = this.dgEmail.Items[0];

 

}

}

 

else

 

{

 

foreach (ExchangeResultItem item in ViewModel.SelectedEmailResults)

 

{

 

this.dgEmail.SelectedItems.Add(item);

 

}

}

app.RaiseTaskComplete();

}

}

0
Vlad
Telerik team
answered on 14 Oct 2010, 07:58 AM
Hello,

 Why not use MVVM to achieve your goal? It will be better if you introduce SelectedItem property in your model and bind the grid SelectedItem to this property. I've attached an example project to illustrate you this. Instead service I've used BackgroundWorker to simulate asynchronous data retrieving. 

Greetings,
Vlad
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Todd Millett
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Todd Millett
Top achievements
Rank 1
Share this question
or