【问题标题】:WPF Datagrid rows do not get cleared - C#WPF Datagrid 行不会被清除 - C#
【发布时间】:2017-02-12 08:26:41
【问题描述】:

我有一个datagrid;Printreport 通过observable collection 填充; PopulatePatternData 它运行良好,并且在程序运行时显示所有行。 当程序重新运行时,我想用新的数据行更新数据网格,而是用以前的结果(行)添加新数据

XAML:

<DataGrid x:Name="PrintReport" ItemsSource="{Binding PopulatePatternData}" AutoGenerateColumns="False" FontFamily="Tahoma" FontSize="12" CanUserSortColumns="False"
                                                     HorizontalContentAlignment="Stretch"  VerticalContentAlignment="Stretch"  AlternatingRowBackground="Gainsboro"  AlternationCount="1" 
                                                     SelectionMode="Extended" SelectionUnit="Cell" >

我用来运行程序的按钮 (RunAnalysis) 有一个event handler。我在单击 observable collection 时清除它,然后创建数据网格。 我尝试“clear”如下所示的数据网格行,但无济于事。

 private void RunAnalysis(object sender, RoutedEventArgs e)
        {
            WFPlanningCompliancePluginModel model = DataContext as WFPlanningCompliancePluginModel;
            model.PopulatePatternData.Clear();
            PrintReport.Items.Clear();   // This does not work         
            model.Run();
        }

collection 被清除,但datagrid 没有被清除!我错了什么? 如果我使用,程序第一次运行时会失败

PrintReport.Items.Clear(); as it does not find any items. 

【问题讨论】:

  • 如果绑定正常,就不需要 PrintReport.Items.Clear(); - 你能展示模型的代码吗?
  • 如果您正在清除数据库表,那么您需要再次刷新您的数据网格。

标签: c# wpf datagrid


【解决方案1】:

清除物品后重新绑定DataGrid

private void RunAnalysis(object sender, RoutedEventArgs e)
{
    WFPlanningCompliancePluginModel model = DataContext as WFPlanningCompliancePluginModel;
    model.PopulatePatternData.Clear();

    // PrintReport.Items.Clear();   // This you can skip if the binding works.
    PrintReport.DataBind(); // This should be enough if the binding works correctly!
    model.Run();
}

【讨论】:

    猜你喜欢
    • 2012-11-04
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 2011-07-03
    • 2015-10-04
    • 1970-01-01
    • 2018-07-09
    相关资源
    最近更新 更多