WPF MVVM将DataTable绑定到DataGrid不会显示数据 [英] WPF MVVM Bind DataTable to DataGrid does not display data

查看:596
本文介绍了WPF MVVM将DataTable绑定到DataGrid不会显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的控件,其中包含一个DataGrid,并将ItemsSource绑定到一个DataTable。当我填充DataTable时,可以看到在DataGrid中添加了行,但是没有显示任何数据。我没有对此DataGrid使用任何特殊样式(采用默认样式),唯一的设置是AutoGenerateColumn设置为True。

I've got a simple control which contains a DataGrid which ItemsSource is bound to a DataTable. When I fill the DataTable, I can see that rows are added in the DataGrid, but no data is displayed. I don't use any special style for this DataGrid (take the default one), the only setting is the AutoGenerateColumn set to True.

在XAML中

<DataGrid AutoGenerateColumns="True" ItemsSource="{Binding TableResult}"/>

在视图模型中

private DataTable tableResult = new DataTable();
public DataTable TableResult
{
   get { return tableResult; }
   set { tableResult = value; OnPropertyChanged("TableResult"); }
}

private void FillTable()
{
   DataColumn c = new DataColumn();
   c.ColumnName = "Col1";
   this.TableResult.Columns.Add(c);

   c = new DataColumn();
   c.ColumnName = "Col2";
   this.TableResult.Columns.Add(c);

   DataRow row1 = this.TableResult.NewRow();
   row1["Col1"] = "Blue";
   row1["Col2"] = "12";;
   this.TableResult.Rows.Add(row1);

   DataRow row2 = this.TableResult.NewRow();
   row2["Col1"] = "Red";
   row2["Col2"] = "18";
   this.TableResult.Rows.Add(row2);

   DataRow row3 = this.TableResult.NewRow();
   row3["Col1"] = "Yellow";
   row3["Col2"] = "27";
   this.TableResult.Rows.Add(row3);
}


推荐答案

填充数据的工作示例DataGrid:

Working example of populating data of DataGrid:

    public MyViewModel()//constructor of MyViewModel
    {
        FillMyDataGrid();            
    }

    private DataTable employeeDataTable;

    public DataTable EmployeeDataTable
    {
        get { return employeeDataTable; }
        set
        {
            employeeDataTable = value;
            OnPropertyChanged("EmployeeDataTable");
        }
    }

    public FillMyDataGrid()
    {
            var _ds = new DataSet("Test");
            employeeDataTable = new DataTable();
            employeeDataTable = _ds.Tables.Add("DT");
            for (int i = 0; i < 50; i++)
            {
                //employeeDataTable.Columns.Add(i.ToString() + ".");
                employeeDataTable.Columns.Add(i.ToString());
            }
            for (int i = 0; i < 2; i++)
            {
                var theRow = employeeDataTable.NewRow();
                for (int j = 0; j < 50; j++)
                {                       
                        theRow[j] = "a";

                }
                employeeDataTable.Rows.Add(theRow);
            }
   }

XAML代码:

<DataGrid ItemsSource="{Binding EmployeeDataTable}"/>

更新:

我已经测试了您的示例,并且它可以正常运行,但是您应该在构造函数中调用方法
FillTable()来填充数据表。例如:

I've tested your example and it works correctly, but you should call your method FillTable() in constructor to populate DataTable. For example:

public class YourViewModel
{
    public YourViewModel()
    {  
        FillTable();
    }
}

这篇关于WPF MVVM将DataTable绑定到DataGrid不会显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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