【问题标题】:Edits to dynamic DataTable not updating in DataGrid对动态 DataTable 的编辑未在 DataGrid 中更新
【发布时间】:2012-02-08 22:38:37
【问题描述】:

我有一个 DataTable,当我更改(添加、修改..ect)时,它不会反映在绑定到 DataTable 的 DataGrid 上。

ItemsSource="{Binding TableData,Mode=TwoWay,IsAsync=True}" 

这就是绑定^

现在当我设置 RowError 时

TableData.Rows[x].RowError = ex.Message;

HasError 设置为 true...但 DataGrid 没有反映这一点(我有一个样式,当出现错误时将行标记为红色)

注意:我的更改并没有反映在设置 ErrorMessages 上,我还尝试在 ViewModel 中添加行,并且这些添加的行也没有反映。

关于数据表: 它没有设置列或任何东西,它反映了用户选择的选定数据库表。

【问题讨论】:

    标签: wpf mvvm datagrid datatable


    【解决方案1】:

    绑定到DataTable 将不起作用。您需要先将您的DataTable 转换为ObservableCollection<>。来自here

        public void ConvertDataTable( DataTable dt )
        {
            DataList  = new ObservableCollection<ProjectWorkHours>();
            //Scan and arrange data into ObservableCollection
            int UserID = 0;
            if ( dt.Rows.Count >0 )
            {
                UserID = int.Parse( dt.Rows[0]["UserID"].ToString() );
                //Distill project id list
                List<int> ProjectIDList = GetProjectIDList( dt );
                for ( int i = 0 ; i < ProjectIDList.Count; i ++ )
                {
                    int ProjectID= ProjectIDList[i];
                    //Get WorkRecord
                    int[] MyWorkRecord = GetWorkRecord(dt, ProjectID);
                    ProjectWorkHours newProjectWorkHours = new ProjectWorkHours(UserID,ProjectID,MyWorkRecord);
                    DataList.Add( newProjectWorkHours);
                }           
            } 
        }
    

    该链接有一个更完整的使用数据库和使用绑定的示例。

    【讨论】:

    【解决方案2】:

    TableData 是什么类型?

    如果是List&lt;&gt;,那么更改列表中的项目将不会更新绑定
    如果是ObservableCollection&lt;&gt;,那么更改集合中的项目将更新绑定

    【讨论】:

    • 它是一个DataTable(公共DataTable TableData)
    • 不是 TableDate!查看您与 itemsource 绑定的变量。我在问什么类型是变量。
    猜你喜欢
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2012-11-01
    相关资源
    最近更新 更多