DataTable is not updating DataGrid after clearing and refilling data. MVVM

≯℡__Kan透↙ 提交于 2019-12-25 06:26:25

问题


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);        
       }
    }
}

My xaml is:

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

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


回答1:


DataTable itself is not observable. Therefore UI is not notified about the changes you make. You have a few options to refresh the data:

  • You can wrap the entries of the DataTable with an ObservableCollection and bind your DataGrid to this collection. But, you have to synchronize the data between DataTable and ObservableCollection.
  • In the codebehind you can call DataGrid.Items.Refresh() method when your button is clicked.
  • You can set the ItemsSource property of the DataGrid to null and again to your DataTable:

Example:

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);        
    }

    DataTable tempDataTable = EmployeeDataTable;
    EmployeeDataTable = null;
    EmployeeDataTable = tempDataTable;
}



回答2:


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");
    }

and 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>


来源:https://stackoverflow.com/questions/36215919/datatable-is-not-updating-datagrid-after-clearing-and-refilling-data-mvvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!