当DataGrid使用对象进行了分组时候,该对象类型必须实现 IComparable 接口,否则出错:必须至少有一个对象实现。

 

代码如下:

 


            ObservableCollection
<TaskProject> list1 = new ObservableCollection<TaskProject>();
            
for (int i = 1; i <= 2; i++)
            {
                list1.Add(
new TaskProject()
                {
                    ProjectId 
= i,
                    ProjectName 
= "Project " + i.ToString(),
                });
            }
            list1.Add(
new TaskProject() { ProjectId = 1, ProjectName = "Project 1" });
            
this.dataGrid2.ItemsSource = new PagedCollectionView(list1);

            ObservableCollection
<Task> taskList = new ObservableCollection<Task>();
            
// Generate some task data and add it to the task list.
            for (int i = 1; i <= 100; i++)
            {
                taskList.Add(
new Task()
                {
                    Area 
= i / 3D,
                    ProjectName 
= "公司 " + ((i % 5+ 1).ToString(),
                    TaskName 
= "Task " + i.ToString(),
                    Project 
= list1[i % list1.Count],
                    DueDate 
= DateTime.Now.AddDays(i),
                    Complete 
= (i % 2 == 0),
                    Notes 
= "Task " + i.ToString() + " is due on "
                          
+ DateTime.Now.AddDays(i) + ". Lorum ipsum..."
                });
            }

            PagedCollectionView taskListView 
= new PagedCollectionView(taskList);
            taskListView.CollectionChanged 
+= new System.Collections.Specialized.NotifyCollectionChangedEventHandler(taskListView_CollectionChanged);
            
this.dataGrid1.ItemsSource = taskListView;


            
if (taskListView.CanGroup == true)
            {
                PropertyGroupDescription group 
= new PropertyGroupDescription("Project");
                group.Converter 
= new ProjectConverter();
                taskListView.GroupDescriptions.Add(group);
                
// Then group by Complete status.
                taskListView.GroupDescriptions.Add(new PropertyGroupDescription("Complete"));
            }
            
if (taskListView.CanSort == true)
            {
                
// By default, sort by ProjectName.
                taskListView.SortDescriptions.Add(new SortDescription("ProjectName", ListSortDirection.Ascending));
            }

            dataPager1.Source 
= taskListView;

相关文章:

  • 2022-01-13
  • 2022-12-23
  • 2021-09-21
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2021-06-24
猜你喜欢
  • 2022-12-23
  • 2021-08-28
  • 2021-08-25
  • 2022-12-23
  • 2021-05-23
  • 2021-09-11
相关资源
相似解决方案