[英]Reload DataTable bound to Datagrid in WPF
I want to display a SQL table in my WPF Datagrid, writing SQL query at runtime. 我想在我的WPF Datagrid中显示一个SQL表,在运行时编写SQL查询。 I've built my View (simplified) and ViewModel associated: 我已经构建了我的View(简化)和ViewModel关联:
View: 视图:
-----------------MyDataGrid.xaml----------------- ----------------- MyDataGrid.xaml -----------------
<DataGrid x:Name="CurrentSqlGrid"
ItemsSource="{Binding Path=CurrentDataTable,Mode=OneWay}"
Grid.Row="2" AutoGenerateColumns="True" IsReadOnly="True" />
<TextBox x:Name="TextBoxSql" Height="120" Grid.Row="1"
TextWrapping="Wrap" VerticalAlignment="Top"/>
-----------------MyDataGrid.cs----------------- ----------------- MyDataGrid.cs -----------------
public partial class MyDataGrid : UserControl
{
private SqlDataGridViewModel sqlDataGridViewModel;
public MyDataGrid()
{
InitializeComponent();
sqlDataGridViewModel = new SqlDataGridViewModel();
this.DataContext = sqlDataGridViewModel;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
sqlDataGridViewModel.FillDataTableWithQuery(TextBoxSql.Text);
}
}
ViewModel: 视图模型:
-----------------SqlDataGridViewModel.cs----------------- ----------------- SqlDataGridViewModel.cs -----------------
public class SqlDataGridViewModel
{
private DataTable _dataTable;
private SqlDataAdapter _adapter;
public DataTable CurrentDataTable
{
get
{
return _dataTable;
}
set
{
_dataTable = value;
}
}
public SqlDataGridViewModel()
{
_dataTable = new DataTable("QUERY");
FillDataTableWithQuery("SELECT TOP 500 * FROM XUSER");
}
public void FillDataTableWithQuery(string query)
{
CurrentDataTable.Reset();
CurrentDataTable.Clear();
try
{
using (SqlConnection sqlConn = new SqlConnection(SessionProvider.Instance.currentConnectionString))
{
_adapter = new SqlDataAdapter(query, sqlConn);
_adapter.Fill(CurrentDataTable);
}
}
catch (Exception ex)
{
MartuLogger.Instance.Error("Exception in SqlDataGridViewModel :" + ex.Message);
}
}
}
This solution display me first DataTable of first query. 此解决方案首先显示第一个查询的DataTable。
But when I write another SQL query, I call FillDataTableWithQuery("SELECT * from another_table")
and Datagrid clears all rows but it doesn't load new columns and new rows! 但是当我编写另一个SQL查询时,我调用FillDataTableWithQuery("SELECT * from another_table")
并且Datagrid清除所有行,但它不会加载新列和新行! (only display old columns!). (仅显示旧列!)。 In debug, the CurrentDataTable
contains new query's results.... Any ideas? 在调试中, CurrentDataTable
包含新查询的结果....任何想法?
SOLUTION With Help of Faisal Hafeez we 've found this solution: 解决方案在Faisal Hafeez的帮助下,我们找到了这个解决方案:
Xaml and code behind are the same. Xaml和后面的代码是一样的。 it work with OneWay and TwoWay mode. 它适用于OneWay和TwoWay模式。
My view model becomes : 我的视图模型变为:
public class SqlDataGridViewModel : INotifyPropertyChanged
{
private DataTable _dataTable;
private SqlDataAdapter _adapter;
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChange(string PropertyName)
{
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
}
public DataTable CurrentDataTable
{
get
{
return _dataTable;
}
set
{
_dataTable = value;
//NotifyPropertyChange("CurrentDataTable");
}
}
public SqlDataGridViewModel()
{
_dataTable = new DataTable("QUERY");
FillDataTableWithQuery("SELECT TOP 500 * FROM XUSER");
}
public void FillDataTableWithQuery(string query)
{
_dataTable = new DataTable("QUERY");
try
{
using (SqlConnection sqlConn = new SqlConnection(SessionProvider.Instance.currentConnectionString))
{
_adapter = new SqlDataAdapter(query, sqlConn);
_adapter.Fill(CurrentDataTable);
}
NotifyPropertyChange("CurrentDataTable");
}
catch (Exception ex)
{
MartuLogger.Instance.Error("Exception in SqlDataGridViewModel :" + ex.Message);
}
}
}
If i don't NotifyPropertyChange after _adapter.Fill, view doesn't refresh. 如果我在_adapter.Fill之后没有NotifyPropertyChange,则视图不会刷新。 (I've tried to notify in Set CurrentTable, but _adapter.Fill evidently don't call CurrentTable's set method). (我试图在Set CurrentTable中通知,但_adapter.Fill显然不会调用CurrentTable的set方法)。 Thank you. 谢谢。
Try this. 尝试这个。 You need to implement INotifyPropertyChanged and call PropertyChanged on set event of datagrid. 您需要实现INotifyPropertyChanged并在datagrid的set事件上调用PropertyChanged。
public class SqlDataGridViewModel : INotifyPropertyChanged { public class SqlDataGridViewModel:INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public DataTable CurrentDataTable
{
get
{
return _dataTable;
}
set
{
_dataTable = value;
OnPropertyChanged("CurrentDataTable");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.