清除并重新填充数据后,DataTable不会更新DataGrid。 MVVM [英] DataTable is not updating DataGrid after clearing and refilling data. MVVM

查看:145
本文介绍了清除并重新填充数据后,DataTable不会更新DataGrid。 MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的viewModel是:

My viewModel is:

 public class MainWindowViewModel
 {
    public MainWindowViewModel()
    {
        PopulateDataTable();
    }
    private DataTable employeeDataTable;
    public DataTable EmployeeDataTable
    {
        get { return employeeDataTable; }
        set
        {
            employeeDataTable = value;
            OnPropertyChanged("EmployeeDataTable");
        }
    }

    private void PopulateDataTable()
    {            
        var _ds = new DataSet("Test");
        employeeDataTable = new DataTable();
        employeeDataTable = _ds.Tables.Add("DT");
        for (int i = 0; i < 10; i++)
        {
            employeeDataTable.Columns.Add("Column " + i.ToString());
        }
        for (int i = 0; i < 15; i++)
        {
            var theRow = employeeDataTable.NewRow();
            for (int j = 0; j < 10; j++)
            {
              theRow[j] = "a";                 
            }
            employeeDataTable.Rows.Add(theRow);
        }
    }

    private void RepopulateDataTable(object obj)
    {
          EmployeeDataTable.Clear();
          for (int i = 0; i < 10; i++)
          {
              var theRow = employeeDataTable.NewRow();
              for (int j = 0; j < 10; j++)
              {
                  theRow[j] = j + DateTime.Now.ToString();
              }
              employeeDataTable.Rows.Add(theRow);        
       }
    }
}

我的xml是:

<DataGrid ItemsSource="{Binding EmployeeDataTable, IsAsync=True}" />
<Button Content="Update DataGrid" Command={Binding UpdateDataGridCommand}/>

当我调用方法 RepopulateDataTable() Button ,则 DataGrid 中的数据永不更新。如何使用 DataTable 清除并重新填充 DataGrid

When I call method RepopulateDataTable() by Button , then data in DataGrid never updates. How can I clear and repopulate DataGrid using DataTable?

推荐答案

我只是重新发布

I am just reposting Andy ONeill's answer from WPF MSDN forum. Andy's answer works like a charm:

public class MainWindowViewModel:ViewModelBase
{
    public MainWindowViewModel()
    {
        PopulateDataTable();
        RepopulateCommand = new RelayCommand(RepopulateDataTable);
    }
    private DataTable employeeDataTable;
    public DataView EmployeeDataView
    {
        get { return employeeDataTable.DefaultView; }
    }       

    public RelayCommand RepopulateCommand { get; set; }


    private void PopulateDataTable()
    {
        employeeDataTable = new DataTable();
        for (int i = 0; i < 10; i++)
        {
            employeeDataTable.Columns.Add("Column " + i.ToString());
        }
        for (int i = 0; i < 15; i++)
        {
            var theRow = employeeDataTable.NewRow();
            for (int j = 0; j < 10; j++)
            {
                theRow[j] = "a";
            }
            employeeDataTable.Rows.Add(theRow);
        }
        OnPropertyChanged("EmployeeDataView");
    }

    private void RepopulateDataTable(object obj)
    {
        employeeDataTable.Clear();
        for (int i = 0; i < 10; i++)
        {
            var theRow = employeeDataTable.NewRow();
            for (int j = 0; j < 10; j++)
            {
                theRow[j] = j + DateTime.Now.ToString();
            }
            employeeDataTable.Rows.Add(theRow);
        }
        OnPropertyChanged("EmployeeDataView");
    }

和XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>
    <DataGrid ItemsSource="{Binding EmployeeDataView}"/>
    <Button Grid.Row="1"
            Content="RePopulate"
            Command="{Binding RepopulateCommand}"/>            
</Grid>

这篇关于清除并重新填充数据后,DataTable不会更新DataGrid。 MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆