DataViewRowState 列舉
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
描述 DataRow 中的資料版本。
此列舉支援其成員值的位元組合。
public enum class DataViewRowState
[System.Flags]
public enum DataViewRowState
[<System.Flags>]
type DataViewRowState =
Public Enum DataViewRowState
- 繼承
- 屬性
欄位
Added | 4 | 新的資料列。 |
CurrentRows | 22 | 目前的資料列,這些資料列包含未變更的、新的和修改的資料列。 根據預設,DataViewRowState 會設為 CurrentRows。 |
Deleted | 8 | 刪除的資料列。 |
ModifiedCurrent | 16 | 已經修改之原始資料目前的版本 (請參閱 |
ModifiedOriginal | 32 | 已修改之資料的原始版本 (雖然已經過修改,這些資料還是以 |
None | 0 | 無。 |
OriginalRows | 42 | 原始資料列,這些資料列包含未變更和已刪除的資料列。 |
Unchanged | 2 | 未變更的資料列。 |
範例
在下列範例 DataTable 中,會使用單一數據行建立。 數據會變更,而的 RowStateFilterDataView 會設定為顯示不同的數據列集,視 DataViewRowState而定。
static private void DemonstrateRowState()
{
// Create a DataTable with one column.
DataTable dataTable = new DataTable("dataTable");
DataColumn dataColumn = new DataColumn("dataColumn");
dataTable.Columns.Add(dataColumn);
// Add ten rows.
DataRow dataRow;
for (int i = 0; i < 10; i++)
{
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "item " + i;
dataTable.Rows.Add(dataRow);
}
dataTable.AcceptChanges();
// Create a DataView with the table.
DataView dataView = new DataView(dataTable);
// Change one row's value:
dataTable.Rows[1]["dataColumn"] = "Hello";
// Add one row:
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "World";
dataTable.Rows.Add(dataRow);
// Set the RowStateFilter to display only added and modified rows.
dataView.RowStateFilter = DataViewRowState.Added
| DataViewRowState.ModifiedCurrent;
// Print those rows. Output = "Hello" "World";
PrintView(dataView, "ModifiedCurrent and Added");
// Set filter to display on originals of modified rows.
dataView.RowStateFilter = DataViewRowState.ModifiedOriginal;
PrintView(dataView, "ModifiedOriginal");
// Delete three rows.
dataTable.Rows[1].Delete();
dataTable.Rows[2].Delete();
dataTable.Rows[3].Delete();
// Set the RowStateFilter to display only Added and modified rows.
dataView.RowStateFilter = DataViewRowState.Deleted;
PrintView(dataView, "Deleted");
//Set filter to display only current.
dataView.RowStateFilter = DataViewRowState.CurrentRows;
PrintView(dataView, "Current");
// Set filter to display only unchanged rows.
dataView.RowStateFilter = DataViewRowState.Unchanged;
PrintView(dataView, "Unchanged");
// Set filter to display only original rows.
dataView.RowStateFilter = DataViewRowState.OriginalRows;
PrintView(dataView, "OriginalRows");
}
static private void PrintView(DataView dataView, string label)
{
Console.WriteLine("\n" + label);
for (int i = 0; i < dataView.Count; i++)
{
Console.WriteLine(dataView[i]["dataColumn"]);
}
}
Private Sub DemonstrateRowState()
Dim i As Integer
' Create a DataTable with one column.
Dim dataTable As New DataTable("dataTable")
Dim dataColumn As New DataColumn("dataColumn")
dataTable.Columns.Add(dataColumn)
' Add ten rows.
Dim dataRow As DataRow
For i = 0 To 9
dataRow = dataTable.NewRow()
dataRow("dataColumn") = "item " + i.ToString()
dataTable.Rows.Add(dataRow)
Next i
dataTable.AcceptChanges()
' Create a DataView with the table.
Dim dataView As New DataView(dataTable)
' Change one row's value:
dataTable.Rows(1)("dataColumn") = "Hello"
' Add one row:
dataRow = dataTable.NewRow()
dataRow("dataColumn") = "World"
dataTable.Rows.Add(dataRow)
' Set the RowStateFilter to display only Added and modified rows.
dataView.RowStateFilter = _
DataViewRowState.Added Or DataViewRowState.ModifiedCurrent
' Print those rows. Output = "Hello" "World";
PrintView(dataView, "ModifiedCurrent and Added")
' Set filter to display on originals of modified rows.
dataView.RowStateFilter = DataViewRowState.ModifiedOriginal
PrintView(dataView, "ModifiedOriginal")
' Delete three rows.
dataTable.Rows(1).Delete()
dataTable.Rows(2).Delete()
dataTable.Rows(3).Delete()
' Set the RowStateFilter to display only Added and modified rows.
dataView.RowStateFilter = DataViewRowState.Deleted
PrintView(dataView, "Deleted")
'Set filter to display only current.
dataView.RowStateFilter = DataViewRowState.CurrentRows
PrintView(dataView, "Current")
' Set filter to display only unchanged rows.
dataView.RowStateFilter = DataViewRowState.Unchanged
PrintView(dataView, "Unchanged")
' Set filter to display only original rows.
dataView.RowStateFilter = DataViewRowState.OriginalRows
PrintView(dataView, "OriginalRows")
End Sub
Private Sub PrintView(ByVal dataView As DataView, ByVal label As String)
Console.WriteLine(ControlChars.Cr + label)
Dim i As Integer
For i = 0 To dataView.Count - 1
Console.WriteLine(dataView(i)("dataColumn"))
Next i
End Sub
備註
這些 DataViewRowState 值可用來從 DataRow擷取特定版本的數據,或判斷存在哪些版本。
RowStateFilter設定 的 DataView 屬性,以指定要檢視的數據版本。
您可以使用布爾運算符 Or 搭配值來取得多個版本。
DataViewRowState在 DataTable 方法中使用 Select 。