比较2个ObservableCollections并突出显示差异

时间:2011-11-07 14:17:46

标签: c# wpf datagrid

我有2个ObservableCollections我在那里比较数据。如果数据不同,我将任务添加到新的ObservableCollection。现在我的问题是:“有没有办法突出显示一个不同的变量,因此很容易找到它?”。

以下是我的比较代码的一部分:

public void CompareNow()
    {
        Repository onlineRepository = new Repository(App.SharePointSiteLink);
        ObservableCollection<Task> tempLocalTasks = new ObservableCollection<Task>();
        ObservableCollection<Task> tempOnlineTasks = new ObservableCollection<Task>(); 

        tempLocalTasks.Clear();
        tempOnlineTasks.Clear();

        onlineRepository.DownloadTasks();
        onlineRepository.UpdateTasksToShow();
        onlineRepository.UpdateEmployees();
        onlineRepository.UpdateWorkareas();
        onlineRepository.UpdateWorkAreaByEmployee();

        foreach (Task onlineTask in onlineRepository.Tasks)
        {
            foreach (Task localTask in TasksToShow)
            {
                if (localTask.TaskID == onlineTask.TaskID)
                    if (localTask.StartDate != onlineTask.StartDate || localTask.ActualHours != onlineTask.ActualHours || localTask.EstimatedHours != onlineTask.EstimatedHours || localTask.Status != onlineTask.Status || localTask.Priority != onlineTask.Priority || localTask.Category != onlineTask.Category || localTask.TaskName != onlineTask.TaskName)
                    {
                        tempLocalTasks.Add(localTask);
                        tempOnlineTasks.Add(onlineTask);
                    }
            }
        }

        LocalTasks.Clear();
        OnlineTasks.Clear();

        foreach (Task tempLocalTask in tempLocalTasks.OrderBy(x => x.TaskID))
        {
            LocalTasks.Add(tempLocalTask);
        }

        foreach (Task tempOnlineTask in tempOnlineTasks.OrderBy(x => x.TaskID))
        {
            OnlineTasks.Add(tempOnlineTask);
        }

        changesCount = LocalTasks.Count;
    }

这是我的查看代码:

<Grid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="90"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <TextBlock Text="Local tasks" Grid.Column="0" Style="{StaticResource titleStyle}"></TextBlock>
        <TextBlock Text="{Binding ChangesCount}" Grid.Column="1" Style="{StaticResource titleStyle}"></TextBlock>
        <TextBlock Text="Online tasks" Grid.Column="2" Style="{StaticResource titleStyle}"></TextBlock>
        <Button Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Top" Height="20" Width="20" Margin="0,2,5,0" Click="Button_Click">
            <Image Source="/ResourcePlanningTool;component/Images/print.png"></Image>
        </Button>
    </Grid>

    <DockPanel Grid.Column="0" Grid.Row="1">
        <ScrollViewer Name="localScrollViewer" ScrollChanged="localScrollViewer_ScrollChanged" HorizontalScrollBarVisibility="Visible">
            <DataGrid Name="localDatagrid" Grid.ColumnSpan="2" AutoGenerateColumns="False" ItemsSource="{Binding LocalTasks}" IsReadOnly="True" Margin="2" SelectionChanged="localDatagrid_SelectionChanged" PreviewMouseWheel="localDatagrid_PreviewMouseWheel" LoadingRow="localDatagrid_LoadingRow" MouseMove="localDatagrid_MouseMove">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Task ID" Binding="{Binding Path=TaskID}"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=TaskName}" Header="Task name" />
                    <DataGridTextColumn Binding="{Binding Path=TaskResponsible}" Header="Task responsible" />
                    <DataGridTextColumn Binding="{Binding Path=TaskResponsibleDepartment}" Header="Task responsible department"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=Category}" Header="Category" />
                    <DataGridTextColumn Binding="{Binding Path=Status}" Header="Status" ></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=Priority}" Header="Priority" />
                    <DataGridTextColumn Binding="{Binding Path=StartDate, StringFormat={}{0:yyyy MMMM dd}}" Header="Start date"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=ActualHours.TotalHours, StringFormat={}{0:0}}" Header="Actual hours" />
                    <DataGridTextColumn Binding="{Binding Path=EstimatedHours.TotalHours, StringFormat={}{0:0}}" Header="Estimated hours" />
                    <DataGridTextColumn Binding="{Binding Path=EstimatedDeploymentDate, StringFormat={}{0:yyyy MMMM dd}}" Header="Estimated deployment date" />
                    <DataGridTextColumn Binding="{Binding Path=DesiredImplementationDate, StringFormat={}{0:yyyy MMMM dd}}" Header="Desired deployment date" />
                    <DataGridTextColumn Binding="{Binding Path=APP.StartDate, StringFormat={}{0:yyyy MMMM dd}}" Header="APP - Start Date"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=APP.EstimatedHours}" Header="APP - Estimated hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=APP.ActualHours}" Header="APP - Actual hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=IN.StartDate, StringFormat={}{0:yyyy MMMM dd}}" Header="IN - Start Date"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=IN.EstimatedHours}" Header="IN - Estimated hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=IN.ActualHours}" Header="IN - Actual hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=SIS.StartDate, StringFormat={}{0:yyyy MMMM dd}}" Header="SIS - Start Date"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=SIS.EstimatedHours}" Header="SIS - Estimated hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=SIS.ActualHours}" Header="SIS - Actual hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=Tags}" Header="Tags"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </ScrollViewer>
    </DockPanel>

    <DockPanel Grid.Column="1" Grid.Row="1">
        <ScrollViewer Name="onlineScrollViewer" ScrollChanged="onlineScrollViewer_ScrollChanged" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Visible">
            <DataGrid Name="onlineDatagrid" AutoGenerateColumns="False" ItemsSource="{Binding OnlineTasks}" IsReadOnly="True" Margin="2" SelectionChanged="onlineDatagrid_SelectionChanged" PreviewMouseWheel="onlineDatagrid_PreviewMouseWheel">
                <DataGrid.Columns>
                    <DataGridTemplateColumn Header="Task ID" CellTemplate="{StaticResource hyperlinkTemplate}"></DataGridTemplateColumn>
                    <DataGridTextColumn Binding="{Binding Path=TaskName}" Header="Task name" />
                    <DataGridTextColumn Binding="{Binding Path=TaskResponsible}" Header="Task responsible" />
                    <DataGridTextColumn Binding="{Binding Path=TaskResponsibleDepartment}" Header="Task responsible department"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=Category}" Header="Category" />
                    <DataGridTextColumn Binding="{Binding Path=Status}" Header="Status" ></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=Priority}" Header="Priority" />
                    <DataGridTextColumn Binding="{Binding Path=StartDate, StringFormat={}{0:yyyy MMMM dd}}" Header="Start date"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=ActualHours.TotalHours, StringFormat={}{0:0}}" Header="Actual hours" />
                    <DataGridTextColumn Binding="{Binding Path=EstimatedHours.TotalHours, StringFormat={}{0:0}}" Header="Estimated hours" />
                    <DataGridTextColumn Binding="{Binding Path=EstimatedDeploymentDate, StringFormat={}{0:yyyy MMMM dd}}" Header="Estimated deployment date" />
                    <DataGridTextColumn Binding="{Binding Path=DesiredImplementationDate, StringFormat={}{0:yyyy MMMM dd}}" Header="Desired deployment date" />
                    <DataGridTextColumn Binding="{Binding Path=APP.StartDate, StringFormat={}{0:yyyy MMMM dd}}" Header="APP - Start Date"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=APP.EstimatedHours}" Header="APP - Estimated hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=APP.ActualHours}" Header="APP - Actual hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=IN.StartDate, StringFormat={}{0:yyyy MMMM dd}}" Header="IN - Start Date"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=IN.EstimatedHours}" Header="IN - Estimated hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=IN.ActualHours}" Header="IN - Actual hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=SIS.StartDate, StringFormat={}{0:yyyy MMMM dd}}" Header="SIS - Start Date"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=SIS.EstimatedHours}" Header="SIS - Estimated hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=SIS.ActualHours}" Header="SIS - Actual hours"></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Path=Tags}" Header="Tags"></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
        </ScrollViewer>
    </DockPanel>
</Grid>

2 个答案:

答案 0 :(得分:0)

我不认为有一种快速的方法可以做到这一点,但你可以将你的问题减少到这个

foreach (Task onlineTask in onlineRepository.Tasks)
{
    foreach (Task localTask in TasksToShow)
    {
        if ((localTask != onlineTask) && (localTask.TaskID == onlineTask.TaskID))
        {
            tempLocalTasks.Add(localTask);
            tempOnlineTasks.Add(onlineTask);
        }
    }
}

另外,如果你使用for而不是foreach,你只需要一个循环。

for (int i=0; i< TasksToShow.Count(); i++) onlineRepository.Tasks)
{
    if ((TasksToShow!= onlineRepository[i].Tasks) && (TasksToShow.TaskID == onlineRepository.Tasks[i].TaskID))
    {
        tempLocalTasks.Add(TasksToShow);
        tempOnlineTasks.Add(onlineRepository.Tasks[i]);
    }
}

答案 1 :(得分:0)

我找到了另一种方法。我所做的不是有两个结果集合,而是将结果合并到一个集合中,然后只显示有错误的变量。

这是之前的图片

enter image description here

这是

之后的图片

enter image description here