问题
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 anObservableCollection
and bind yourDataGrid
to this collection. But, you have to synchronize the data betweenDataTable
andObservableCollection
. - In the codebehind you can call
DataGrid.Items.Refresh()
method when your button is clicked. - You can set the
ItemsSource
property of theDataGrid
to null and again to yourDataTable
:
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