重新加载DataFable绑定到WPF中的Datagrid

时间:2013-04-15 13:44:09

标签: c# wpf datagrid datatable

我想在我的WPF Datagrid中显示一个SQL表,在运行时编写SQL查询。 我构建了我的View(简化)和ViewModel关联:

查看:

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

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

视图模型:

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

此解决方案首先显示第一个查询的DataTable。

但是当我编写另一个SQL查询时,我调用FillDataTableWithQuery("SELECT * from another_table")并且Datagrid清除所有行,但它不会加载新列和新行! (仅显示旧列!)。在调试中,CurrentDataTable包含新查询的结果....任何想法?

在Faisal Hafeez的帮助下,我们找到了这个解决方案:

Xaml和后面的代码是一样的。它适用于OneWay和TwoWay模式。

我的观点模型变为:

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

如果我在_adapter.Fill之后没有NotifyPropertyChange,则视图不会刷新。 (我试图在Set CurrentTable中通知,但_adapter.Fill显然不会调用CurrentTable的set方法)。 谢谢。

1 个答案:

答案 0 :(得分:1)

试试这个。您需要实现INotifyPropertyChanged并在datagrid的set事件上调用PropertyChanged。

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