Silverlight DomainDataSource in code-behind not loading all rows

2.4k views Asked by At

I have a domain data source in Silverlight 4. I'm setting it up in the code behind, and I'm also using a RadDataPager and RadGridView.

DomainDataSource dds = new DomainDataSource();

dds.QueryName = "MyQueryName";
dds.DomainContext = ctx;
dds.LoadSize = 10;
dds.PageSize = 10;
dds.AutoLoad = true;

radDataPager1.PageSize = 10;
radDataPager1.Source = dds.Data;
radGridView1.ItemsSource = dds.Data;

dds.Load();

The first time this loads, the dds.Data.Count is 8, and only 8 items show up in the grid view, even though the dds.Data.PageSize is 10. After I page to the next page, all 10 objects are loaded like they should be.

I'm new to Silverlight, and have no idea what is going on here. Am I loading the data wrong? Any ideas?

1

There are 1 answers

0
gringo_dave On

This behavior can be caused by wrong set of Key attribute in your model class. If in the result query, rows with Key field are duplicated, then DataGrid will take only first.

Example (book store info ): Your result class (BookPriceInfo.cs) looks like this:

    public class BookPriceInfo {
      [DataMember]
      [Key]
      public int BookId { get; set; }
      [DataMember]
      public string Name {get; set; }
      [DataMember]
      public string StoreName {get; set;}
      [DataMember]
      public decimal Price {get; set;}
   }

And if you have query result returned from database :

BookId | Name | StoreName | Price 
1 | book1 | store1 | 70.00
1 | book1 | store2 | 69.99
2 | book2 | store1 | 40.00
2 | book2 | store2 | 39.99

Then DataGrig will show only this :

BookId | Name | StoreName | Price 
1 | book1 | store1 | 70.00  
2 | book2 | store1 | 40.00

this happens because of DataGrid will 'distinct' the results by field marked as Key(get only first row from all rows with same BookId), because it should be uniq for all rows.

Solution

Remove Key attribute from field which will have duplicated values (BookId) and set it to field which will have uniqe values for all rows (you can add some column like BookPriceId, which will be uniqe).

    public class BookPriceInfo {
      [DataMember]
      public int BookId { get; set; }
      [DataMember]
      public string Name {get; set; }
      [DataMember]
      public string StoreName {get; set;}
      [DataMember]
      public decimal Price {get; set;}
      [DataMember]
      [Key]
      public int BookPriceId {get; set;}
   }

Query result:

BookPriceId | BookId | Name | StoreName | Price 
1 | 1 | book1 | store1 | 70.00
2 | 1 | book1 | store2 | 69.99
3 | 2 | book2 | store1 | 40.00
4 | 2 | book2 | store2 | 39.99

After that you should see all rows returned by query. Hope this helps :)