3

我想在我的 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 方法)。谢谢你。

4

1 回答 1

1

尝试这个。您需要实现 INotifyPropertyChanged 并在数据网格的设置事件上调用 PropertyChanged。

公共类 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");
        }
    }
}
于 2013-04-15T14:34:31.133 回答