DataView 相关技术1

DataView 通常用于其中的控件公开基础表视图内容的数据绑定应用程序。以下主题讨论如何向自定义控件或在未数据绑定的应用程序中公开 DataView 的内容,其中包括如何在 DataView 中搜索特定值以及如何基于父子关系创建子数据的视图。

本节内容

查看 DataView 的内容:描述如何访问由 DataView 公开的数据。

DataView 公开可枚举的 DataRowView 对象集合。DataRowView 对象将值公开为 object 数组,这些数组按基础表中列的名称或序号引用来编制索引。可以使用 DataRowView 的 Row 属性来访问由 DataRowView 公开的 DataRow。
当使用 DataRowView 查看值时,DataView 的 RowStateFilter 属性将确定公开基础 DataRow 的哪一个行版本。有关使用 DataRow 访问不同行版本的信息,请参阅行状态与行版本。

以下代码示例显示一个表中的所有当前值和初始值。

[Visual Basic]
  Dim catView As DataView = New DataView(catDS.Tables("Categories"))

  Console.WriteLine("Current Values:")

  WriteView(catView)

  Console.WriteLine("Original Values:")

  catView.RowStateFilter = DataViewRowState.ModifiedOriginal

  WriteView(catView)   

Public Shared Sub WriteView(myView As DataView)
  Dim myDRV As DataRowView
  Dim i As Integer

  For Each myDRV In myView
    For i = 0 To myView.Table.Columns.Count - 1
      Console.Write(myDRV(i) & vbTab)
    Next
    Console.WriteLine()
  Next
End Sub

[C#]
  DataView catView = new DataView(catDS.Tables["Categories"]);

  Console.WriteLine("Current Values:");

  WriteView(catView);

  Console.WriteLine("Original Values:");

  catView.RowStateFilter = DataViewRowState.ModifiedOriginal;

  WriteView(catView);

public static void WriteView(DataView myView)
{
  foreach (DataRowView myDRV in myView)
  {
    for (int i = 0; i < myView.Table.Columns.Count; i++)
      Console.Write(myDRV[i] + "/t");
    Console.WriteLine();
  }
}
 

搜索 DataView:描述如何在 DataView 中查找特定行。

使用 DataView 的 Find 和 FindRows 方法,您可以按照行的排序关键字值来对行进行搜索。Find 和 FindRows 方法中的搜索值是否区分大小写取决于基础 DataTable 的 CaseSensitive 属性。搜索值必须完全匹配现有排序关键字值才能返回结果。
Find 方法返回一个整数,该整数表示匹配搜索条件的 DataRowView 的索引。如果多个行匹配搜索条件,则只返回第一个匹配 DataRowView 的索引。如果未找到匹配项,Find 将返回 -1。

若要返回匹配多个行的搜索结果,可以使用 FindRows 方法。FindRows 的工作方式与 Find 方法类似,不同的只是 FindRows 返回引用 DataView 中所有匹配行的 DataRowView 数组。如果未找到匹配项,DataRowView 数组将为空。

若要使用 Find 或 FindRows 方法,必须通过将 ApplyDefaultSort 设置为 true 或通过使用 Sort 属性来指定排序顺序。如果未指定排序顺序,则将引发异常。

Find 和 FindRows 方法将一个值数组用作输入,该数组的长度与排序顺序所包含的列数相匹配。在对单个列进行排序的情况下,可以传递单个值。对于包含多个列的排序顺序,可传递一个对象数组。请注意,当对多个列进行排序时,对象数组中的值必须匹配在 DataView 的 Sort 属性中指定的列的顺序。

以下代码显示对具有单个列排序顺序的 DataView 调用的 Find 方法。

[Visual Basic]
Dim custView As DataView = New DataView(custDS.Tables("Customers"), "","CompanyName", DataViewRowState.CurrentRows)

Dim rowIndex As Integer = custView.Find("The Cracker Box")

If rowIndex = -1 Then
  Console.WriteLine("No match found.")
Else
  Console.WriteLine("{0}, {1}",custView(rowIndex)("CustomerID").ToString(), custView(rowIndex)("CompanyName").ToString())
End If

[C#]
DataView custView = new DataView(custDS.Tables["Customers"], "", "CompanyName", DataViewRowState.CurrentRows);

int rowIndex = custView.Find("The Cracker Box");

if (rowIndex == -1)
  Console.WriteLine("No match found.");
else
  Console.WriteLine("{0}, {1}",custView[rowIndex]["CustomerID"].ToString(),custView[rowIndex]["CompanyName"].ToString());

如果 Sort 属性指定多个列,则必须按 Sort 属性指定的顺序为每个列传递包含搜索值的对象数组,如以下代码示例所示。

[Visual Basic]
Dim custView As DataView = New DataView(custDS.Tables("Customers"), "", "CompanyName, ContactName", DataViewRowState.CurrentRows)

Dim foundRows() As DataRowView = custView.FindRows(New object() {"The Cracker Box", "Liu Wong"})

If foundRows.Length = 0 Then
  Console.WriteLine("No match found.")
Else
  Dim myDRV As DataRowView
  For Each myDRV In foundRows
    Console.WriteLine("{0}, {1}", myDRV("CompanyName").ToString(), myDRV("ContactName").ToString())
  Next
End If

[C#]
DataView custView = new DataView(custDS.Tables["Customers"], "","CompanyName, ContactName",DataViewRowState.CurrentRows);

DataRowView[] foundRows = custView.FindRows(new object[] {"The Cracker Box", "Liu Wong"});

if (foundRows.Length == 0)
  Console.WriteLine("No match found.");
else
  foreach (DataRowView myDRV in foundRows)
    Console.WriteLine("{0}, {1}", myDRV["CompanyName"].ToString(), myDRV["ContactName"].ToString());
 

使用 DataView 进行关系导航:描述如何使用 DataView 基于父子关系创建数据的视图。

如果 DataSet 中的表之间存在关系,则可以使用 DataRowView 的 CreateChildView 方法为父表中的行创建一个 DataView,该 DataView 包含来自相关子表的行。例如,以下代码显示按 CategoryName 和 ProductName 的字母顺序排序的 Categories 及其相关 Products。
[Visual Basic]
Dim catTable As DataTable = catDS.Tables("Categories")
Dim prodTable As DataTable = catDS.Tables("Products")

' Create a relation between the Categories and Products tables.
Dim catProdRel As DataRelation = catDS.Relations.Add("CatProdRel", catTable.Columns("CategoryID"), prodTable.Columns("CategoryID"))

' Create DataViews for the Categories and Products tables.
Dim catView As DataView = New DataView(catTable, "", "CategoryName", DataViewRowState.CurrentRows)
Dim prodView As DataView

' Iterate through the Categories table.
Dim catDRV, prodDRV As DataRowView

For Each catDRV In catView
  Console.WriteLine(catDRV("CategoryName"))

  ' Create a DataView of the child product records.
  prodView = catDRV.CreateChildView(catProdRel)
  prodView.Sort = "ProductName"

  For Each prodDRV In prodView
    Console.WriteLine(vbTab & prodDRV("ProductName"))
  Next
Next

[C#]
DataTable catTable = catDS.Tables["Categories"];
DataTable prodTable = catDS.Tables["Products"];

// Create a relation between the Categories and Products tables.
DataRelation catProdRel = catDS.Relations.Add("CatProdRel", catTable.Columns["CategoryID"],prodTable.Columns["CategoryID"]);

// Create DataViews for the Categories and Products tables.
DataView catView = new DataView(catTable, "", "CategoryName", DataViewRowState.CurrentRows);
DataView prodView;

// Iterate through the Categories table.
foreach (DataRowView catDRV in catView)
{
  Console.WriteLine(catDRV["CategoryName"]);

  // Create a DataView of the child product records.
  prodView = catDRV.CreateChildView(catProdRel);
  prodView.Sort = "ProductName";

  foreach (DataRowView prodDRV in prodView)
    Console.WriteLine("/t" + prodDRV["ProductName"]);
}
 

 

ADO.NET 用行状态和版本管理表中的行。行状态表示行的状态。当修改行时,行版本会维护存储于行中的值,包括当前值、原始值和默认值。例如,修改了某行中的一列后,该行会有一个 Modified 的行状态,并且会存在两个行版本:Current(包含当前行值)和 Original(包含修改该列前的行值)。
每个 DataRow 对象都具有 RowState 属性,您可以检查此属性来确定行的当前状态。下表给出了对各 RowState 枚举值的简要说明。

RowState 说明
Unchanged 自上次调用 AcceptChanges 之后,或自 DataAdapter.Fill 创建了行之后,未做出过任何更改。
Added 已将行添至表中,但尚未调用 AcceptChanges。
Modified 已更改了行的一些元素。
Deleted 已将该行从表中删除,并且尚未调用 AcceptChanges。
Detached 对于已经创建但不属于任何 DataRowCollection 的行,设置为 Detached。新建行的 RowState 设置为 Detached。通过调用 Add 方法将新的 DataRow 添至 DataRowCollection 之后,RowState 属性的值设置为 Added。
对于已经使用 Remove 方法(或是在使用 Delete 方法之后使用了 AcceptChanges 方法)从 DataRowCollection 中移除的行,也设置为 Detached。
 

在 DataSet、DataTable 或 DataRow 上调用 AcceptChanges 时,会移除行状态为 Deleted 的所有行。剩余的行会被赋予 Unchanged 行状态,并且 Original 行版本中的值会改写为 Current 行版本值。调用 RejectChanges 时,会移除行状态为 Added 的所有行。剩余的行会被赋予 Unchanged 的行状态,并且 Current 行版本中的值会改写为 Original 行版本值。

通过用列引用来传递 DataRowVersion 参数,您可以查看行的不同行版本,如下例所示。

[Visual Basic]
Dim custRow As DataRow = custTable.Rows(0)
Dim custID As String = custRow("CustomerID", DataRowVersion.Original).ToString()

[C#]
DataRow custRow = custTable.Rows[0];
string custID = custRow["CustomerID", DataRowVersion.Original].ToString();

下表给出了各 DataRowVersion 枚举值的简要说明。

DataRowVersion 说明
Current 行的当前值。如果是有 Deleted 的 RowState 的行,则不存在此行版本。
Default 特定行的默认行版本。Added、Modified 或 Unchanged 行的默认行版本是 Current。Deleted 行的默认行版本是 Original。Detached 行的默认行版本是 Proposed。
Original 行的原始值。如果是有 Added 的 RowState 的行,则不存在此行版本。
Proposed 行的建议值。在对行进行编辑操作期间,或对于不属于 DataRowCollection 的行,存在此行版本。

通过调用 HasVersion 方法并将 DataRowVersion 作为参数传递,您可以测试 DataRow 是否具有特定的行版本。例如,在调用 AcceptChanges 之前,DataRow.HasVersion(DataRowVersion.Original) 对新添加的行将返回 false。

例如,以下代码示例显示了表中所有已删除行的值。已删除的行没有 Current 行版本,因此在访问列值时必须传递 DataRowVersion.Original。

[Visual Basic]
Dim catTable As DataTable = catDS.Tables("Categories")

Dim delRows() As DataRow = catTable.Select(Nothing, Nothing, DataViewRowState.Deleted)

Console.WriteLine("Deleted rows:" & vbCrLf)

Dim catCol As DataColumn
Dim delRow As DataRow

For Each catCol In catTable.Columns
  Console.Write(catCol.ColumnName & vbTab)
Next
Console.WriteLine()

For Each delRow In delRows
  For Each catCol In catTable.Columns
    Console.Write(delRow(catCol, DataRowVersion.Original) & vbTab)
  Next
  Console.WriteLine()
Next

[C#]
DataTable catTable = catDS.Tables["Categories"];

DataRow[] delRows = catTable.Select(null, null, DataViewRowState.Deleted);

Console.WriteLine("Deleted rows:/n");

foreach (DataColumn catCol in catTable.Columns)
  Console.Write(catCol.ColumnName + "/t");
Console.WriteLine();

foreach (DataRow delRow in delRows)
{
  foreach (DataColumn catCol in catTable.Columns)
    Console.Write(delRow[catCol, DataRowVersion.Original] + "/t");
  Console.WriteLine();
}
 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值