DataView.RowStateFilter 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定在 DataView 使用的資料列狀態篩選。
public:
property System::Data::DataViewRowState RowStateFilter { System::Data::DataViewRowState get(); void set(System::Data::DataViewRowState value); };
public System.Data.DataViewRowState RowStateFilter { get; set; }
[System.Data.DataSysDescription("DataViewRowStateFilterDescr")]
public System.Data.DataViewRowState RowStateFilter { get; set; }
member this.RowStateFilter : System.Data.DataViewRowState with get, set
[<System.Data.DataSysDescription("DataViewRowStateFilterDescr")>]
member this.RowStateFilter : System.Data.DataViewRowState with get, set
Public Property RowStateFilter As DataViewRowState
屬性值
其中一個 DataViewRowState 值。
- 屬性
範例
下列範例會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
備註
只有使用 Delete 方法移除的資料列才會將設定 RowStateFilter 為 Deleted
。 使用 方法新增的資料 AddNew 列,同樣地會將 屬性設定為 Added
。
注意
使用 類別 Remove 的 DataRowCollection 方法並不表示資料列會標示為 Deleted
。 請改用 Delete 方法,以確保可以在 中 DataView檢視這類數據列。
當 設定為 ModifiedCurrent
或 CurrentRows
時RowStateFilter,也會顯示新的數據列。
當 設定為 ModifiedOriginal
和 OriginalRows
時RowStateFilter,也會顯示已刪除的數據列。