绑定的DataTable与DataGrid的WPF中与放大器; MVVM [英] Bind Datatable with DataGrid in WPF & MVVM

查看:161
本文介绍了绑定的DataTable与DataGrid的WPF中与放大器; MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来WPF中,我正在使用WPF MVVM模式,并在运行时产生的数据表结构,然后结合数据表用的DataGrid / RadGridView,这是按预期工作。

I am new to WPF, I am using MVVM pattern in WPF and generating a Datatable structure at the runtime, then binding Datatable with DataGrid/RadGridView, which is working as expected.

问题:我想的DataGrid / RadGridView配置(其中用户可以添加新行,删除行和编辑数据)和保存按钮,点击一切都应该被保存在数据库中。我有一个情况我需要创建不同的列中插入空的DataTable(根据用户输入)。然后这些列下输入值,然后点击保存需要保存在数据库中的价值。我能够绑定的DataTable到DataGrid中(我可以看到所有的列名和数据行如果在网格DataTable中已有的数据),但不能添加(进入),或在从数据网格的运行时间删除任何一行。我已经为CanUserDeleteRows和CanUserInsertRows真值。我不知道我要去的地方错了。我在我的ViewModel类实现INotifyPropertyChanged。

Problem: I want DataGrid/RadGridView configurable (where user can add new row, delete row and edit the data) and on save button click everything should be saved in the database. I have a situation where I need to create an empty DataTable with different columns (depending on user input). And then enter values under those columns and then on click on save need to save value in the database. I am able to bind DataTable to DataGrid (I can see all the column names and data rows if there is already some data in the DataTable in the grid) but NOT able to add (enter) or delete any row at the run time from DataGrid. I have set "True" value for CanUserDeleteRows and CanUserInsertRows. I am not sure where I am going wrong. I am implement INotifyPropertyChanged in my ViewModel class.

我在努力得到期望的结果。

I am struggling to get the desired result.

我的代码看起来如下:

视图模型 -

    DataTable _manualDataTable;
    public DataTable ManualDataTable
    {
        get
        {
            return _manualDataTable;
        }
        set
        {
            _manualDataTable = value;
            OnPropertyChanged("ManualDataTable");
        }
    }

有关创建数据表 -

For creating DataTable--

    void LoadManualDataTable()
    {
        DataTable dtData = new DataTable();
        dtData.Columns.Add("TimeStamp", typeof(DateTime));
        List<DataColumn> columns = new List<DataColumn>();
        var query = _dataContext.GetSenData().Where(sen => sen.LogID == ((DataLogs)SelectedItemNode).Logger.LogID).Select(sen => sen.SeriesID);
        var queryTS = _dataContext.GetDataSeries().Where(ts => query.Contains(ts.SeriesID));
        foreach (DataSeries ts in queryTS)
        {
            var queryPLoc = _dataContext.GetDataLoc().Where(pLoc => pLoc.ParamID == ts.ParamID).Select(pLoc => pLoc.Name);
            dtData.Columns.Add(queryPLoc.First(), typeof(string));
        }

        ManualDataTable = dtData;
     }



XAML代码 -

XAML code--

     <DataGrid Grid.Row="0" AutoGenerateColumns="True" ItemsSource="{Binding ManualDataTable}" CanUserAddRows="True" CanUserDeleteRows="True" IsReadOnly="False"  Name="dataGridManualData"/>



不出所料创建数据表(从LoadManualDataTable法),如果我将通过代码添加任何行则这些行会被绑定,并显示在DataGrid中。但我不能够创建或通过DataGrid中删除行。

DataTable is created as expected (from LoadManualDataTable method) and if I will add any row through the code then those rows will be binded and will be displayed in DataGrid. But I am not able to create or delete rows through DataGrid.

任何帮助将不胜感激。

提前致谢!

推荐答案

我创建一个简单 testproject让你得到了什么,但它的作品没有任何问题。也许你应该张贴您的代码和国家什么行不通

i create a simple testproject to get what you get, but its works without any problem. maybe you should post your code and state what does not work.

public partial class MainWindow : Window
{
    public DataTable MyTable { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        this.MyTable= new DataTable();
        this.MyTable.Columns.Add("Test");
        var row1 = this.MyTable.NewRow();
        row1["Test"] = "dsjfks";

        this.MyTable.Rows.Add(row1);

        this.DataContext = this;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("rows: " + this.MyTable.Rows.Count);
    }

}



XAML

xaml

<DockPanel>
    <Button Height="23" Content="sdf" Click="Button_Click" />
    <DataGrid ItemsSource="{Binding MyTable}" CanUserAddRows="True"/>
</DockPanel>

这篇关于绑定的DataTable与DataGrid的WPF中与放大器; MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆