.Net DataView和DataTable绑定 [英] .Net DataView and DataTable Binding

查看:205
本文介绍了.Net DataView和DataTable绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Windows Forms应用程序将DataView绑定到ListBox。这个DataView使用Linq按照特定列降序对DataTable进行排序。然后我的ListBox绑定到DataView。然后我有一个简单的表单来添加数据到DataTable。当我将DataRow添加到DataTable中时,它会显示在ListBox中。



我很好奇幕后发生了什么... 我已阅读


DataView对象是单个数据表的自定义视图
,可以是
过滤或排序。数据视图是复杂绑定
控件使用的
数据快照。你可以简单或
复杂绑定数据
视图中的数据,但要注意,你是
绑定到
数据的固定图片,而不是一个干净的,更新的
数据源。


我会以为,因为DataView是一个快照有自动更新。当DataTable基础修改时,DataView会添加一个事件进行更新吗?不要误会我,这是我想要的工作,但总是这样吗?

解决方案

code> DataView 不是快照。随着底层 DataTable 更改,它将自动更新。添加到满足 DataView 的过滤条件的 DataTable 的新行将自动显示在 DataView 。类似地,从 DataTable 中删除​​的行将自动从 DataView 中消失。



以下说明了即使使用LINQ时, DataView 的动态特性:

 使用系统; 
使用System.Linq;
使用System.Data;

命名空间ConsoleApplication1
{
类程序
{
static void Main(string [] args)
{
DataTable dt = new DataTable();
dt.Columns.Add(FirstName);

var query = from dt.AsEnumerable()
其中row.Field< string>(FirstName)。StartsWith(S)
select row;

DataView view = query.AsDataView();

Console.WriteLine(view.Count); // Prints 0
dt.Rows.Add(Sam);
dt.Rows.Add(John);
Console.WriteLine(view.Count); //打印1
dt.Rows.Add(Sally);
dt.Rows.Add(Mary);
Console.WriteLine(view.Count); //打印2
dt.Rows.RemoveAt(0);
Console.WriteLine(view.Count); //打印1
}
}
}




DataView添加一个事件来更新底层DataTable?


这是一个内部的实现细节,但是使用事件似乎是可信的。



请注意,您可以使用 DataTable.Copy 方法来复制 DataTable ,如果您真的要创建一个 DataTable 的快照。


I have a simple Windows Forms application which binds a DataView to a ListBox. This DataView uses Linq to sort my DataTable by a specific column descending. My ListBox is then bound to the DataView. I then have a simple form to add data to the DataTable. When I add a DataRow to the DataTable it displays in the ListBox.

I'm curious as to whats going on behind the scenes... I've read:

A DataView object is a customized view of a single data table that may be filtered or sorted. A data view is the data "snapshot" used by complex-bound controls. You can simple- or complex-bind to the data within a data view, but be aware that you are binding to a fixed "picture" of the data rather than a clean, updating data source.

I would have thought that since the DataView is a "snapshot" it wouldn't have automatically updated. Does a DataView add an event to update when the underlying DataTable is modified? Don't get me wrong, this is how I want it to work, but is this always the case?

解决方案

The DataView is not a snapshot. It is updated automatically and immediately as the underlying DataTable changes. New rows added to the DataTable that meet the DataView's filter criteria will automatically appear in the DataView. Similarly, rows removed from the DataTable will automatically disappear from the DataView.

The following illustrates the dynamic nature of the DataView even when using LINQ:

using System;
using System.Linq;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName");

            var query = from row in dt.AsEnumerable()
                        where row.Field<string>("FirstName").StartsWith("S")
                        select row;

            DataView view = query.AsDataView();

            Console.WriteLine(view.Count); // Prints 0
            dt.Rows.Add("Sam");
            dt.Rows.Add("John");
            Console.WriteLine(view.Count); // Prints 1
            dt.Rows.Add("Sally");
            dt.Rows.Add("Mary");
            Console.WriteLine(view.Count); // Prints 2
            dt.Rows.RemoveAt(0);
            Console.WriteLine(view.Count); // Prints 1
        }
    }
}

Does a DataView add an event to update when the underlying DataTable is modified?

This is an internal implementation detail, but it is plausible that this uses events.

Note that you can use the DataTable.Copy method to copy a DataTable, if you really want to create a snapshot of the DataTable.

这篇关于.Net DataView和DataTable绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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