2011-04-29 89 views
5

我有這些2類:PagedViewCollection基,不屬性

class EmpIDPayTypeSettings 
{ 
    public Guid OID { get; set; } 
    public Guid PayTypeOID { get; set; } 
    public int GroupTypeID { get; set; } 
    public Guid EmpOID { get; set; } 
    public int OrderBy { get; set; } 
} 

class DocRegHour 
{ 
    public Guid OID { get; set; }    
    public Guid DocumentOID { get; set; }  
    public Guid medarbejderOID { get; set; } 
    public Guid PaytypeOID { get; set; }  
    public string PayTypeInfo { get; set; } 
    public double Hours { get; set; } 
    public string Comment { get; set; } 
    public DateTime CreatedDate { get; set; } 
    public DateTime LastChangedDate { get; set; }  
} 

我有綁定到的DataGrid的ItemSource的DocRegHour集合。 我希望他們按GroupTypeID分組,因爲您可以看到兩個類共有PaytypeOID

我試着用Header = "Group"製作一個無界限的摺疊列,並將GroupTypeID設置爲每行中的文本。 然後我寫道:

var pcv = new PagedCollectionView(dataGridDayView.ItemsSource); 
pcv.GroupDescriptions.Add(new PropertyGroupDescription("Group")); 
dataGridDayView.ItemsSource = pcv; 

雖然它不工作。任何幫助?

澄清。我有:

IEnumerable<DocRegHour> data; 
IENumerable<EmpIDPayTypeSettings> userSettings; 

var pcv = new PagedCollectionView(data); 
dataGridDayView.ItemsSource = pcv; 

這一點我通過GroupTypeID!這是一個不同的集合

回答

1

如果你想組:

// LINQ the stuff you need and then group by the name you give it 
myData.Where(...).Select(emp => new { Group = emp.GroupTypeID }); 

GroupDescriptor descriptor = new GroupDescriptor("Group"); 
pcv.GroupDescriptors.Add(descriptor); 

它應該工作。儘管我理解你的數據模型,但我並不完全確定。不過,我發現匿名類型在類似情況下非常方便:)

+0

但我不想要某種變異類。我需要很多次從行中提取數據。比如(SelectedItem as DocRecHour),我不能這樣做,用你的方法。另外,如果數據網格有動態列呢? – CasperT 2011-04-29 13:17:51

+0

好吧,這是可以理解的。你有NavigationProperties嗎?如果是的話,你可以使用它們。 – LueTm 2011-04-29 13:24:42

+0

對不起,這些是什麼?我無法在msdn上找到它們 – CasperT 2011-05-02 06:04:07

1

您的問題是,PropertyGroupDescriptor需要propertyName按項分組,而不是分組依據的列標題。

您的類DocRegHour沒有名爲「Group」的屬性,因此PagedCollectionView無法按此屬性對項目進行分組。

要解決此問題,請爲您的類添加屬性「Group」或爲PropertyGroupDescriptor指定另一個屬性。