DataRow.BeginEdit メソッド
アセンブリ: System.Data (system.data.dll 内)



DataRow を編集モードにするには、BeginEdit メソッドを使用します。このモードでは、イベントが一時的に中断されるため、ユーザーは検証規則をトリガせずに 1 つ以上の行に対して複数の変更を行うことができます。たとえば、ある行について合計額の列の値が借方列と貸方列の値に等しいことを確認する必要がある場合、各行を編集モードにして、ユーザーが値をコミットするまで検証を中断できます。
ユーザーがデータ連結コントロールの値を変更すると、BeginEdit メソッドが暗黙的に呼び出されます。DataTable オブジェクトの AcceptChanges メソッドを呼び出すと、EndEdit メソッドが暗黙的に呼び出されます。編集モードの間は、元の値と新しく提示された値が DataRow に格納されています。そのため、EndEdit メソッドが呼び出されるまでは、Item プロパティの version パラメータに DataRowVersion.Original または DataRowVersion.Proposed を渡すことにより、元のバージョンまたは新しく提示されたバージョンのいずれかを取得できます。この時点で編集をキャンセルするには、CancelEdit メソッドを呼び出します。

この例では、DataColumn オブジェクト 1 つ、DataRow オブジェクト 5 つ、およびUniqueConstraint 1 つを使用して単純な DataTable を作成します。この行の値が変更されるのを監視するための RowChanged イベント ハンドラも追加されます。既存の行に対して BeginEdit を呼び出すと、制約とイベントが一時的に無効になり、元の値と提示した値が出力されます。BeginEdit が再び呼び出されて、2 行を同じ値に設定します。EndEdit が呼び出されると、この同一値に対して UniqueConstraint が強制的に適用されます。
Private Sub DemonstrateRowBeginEdit() Dim table As New DataTable("table1") Dim column As New DataColumn("col1", Type.GetType("System.Int32")) AddHandler table.RowChanged, AddressOf Row_Changed table.Columns.Add(column) ' Add a UniqueConstraint to the table. table.Constraints.Add(New UniqueConstraint(column)) ' Add five rows. Dim newRow As DataRow Dim i As Integer For i = 0 To 4 ' RowChanged event will occur for every addition. newRow = table.NewRow() newRow(0) = i table.Rows.Add(newRow) Next i ' AcceptChanges. table.AcceptChanges() ' Invoke BeginEdit on each. Console.WriteLine(ControlChars.Cr _ & " Begin Edit and print original and proposed values " _ & ControlChars.Cr) Dim row As DataRow For Each row In table.Rows row.BeginEdit() row(0) = CInt(row(0)) & 10 Console.Write(ControlChars.Tab & " Original " & ControlChars.Tab _ & row(0, DataRowVersion.Original).ToString()) Console.Write(ControlChars.Tab & " Proposed " & ControlChars.Tab _ & row(0, DataRowVersion.Proposed).ToString() & ControlChars.Cr) Next row Console.WriteLine(ControlChars.Cr) ' Accept changes table.AcceptChanges() ' Change two rows to identical values after invoking BeginEdit. table.Rows(0).BeginEdit() table.Rows(1).BeginEdit() table.Rows(0)(0) = 100 table.Rows(1)(0) = 100 Try ' Now invoke EndEdit. This will cause the UniqueConstraint ' to be enforced. table.Rows(0).EndEdit() table.Rows(1).EndEdit() Catch e As Exception ' Process exception and return. Console.WriteLine("Exception of type {0} occurred.", _ e.GetType().ToString()) End Try End Sub Private Sub Row_Changed _ (sender As Object, e As System.Data.DataRowChangeEventArgs) Dim table As DataTable = CType(sender, DataTable) Console.WriteLine("RowChanged " & e.Action.ToString() _ & ControlChars.Tab & e.Row.ItemArray(0).ToString()) End Sub
private void DemonstrateRowBeginEdit() { DataTable table = new DataTable("table1"); DataColumn column = new DataColumn("col1",Type.GetType("System.Int32")); table.RowChanged+=new DataRowChangeEventHandler(Row_Changed); table.Columns.Add(column); // Add a UniqueConstraint to the table. table.Constraints.Add(new UniqueConstraint(column)); // Add five rows. DataRow newRow; for(int i = 0;i<5; i++) { // RowChanged event will occur for every addition. newRow= table.NewRow(); newRow[0]= i; table.Rows.Add(newRow); } // AcceptChanges. table.AcceptChanges(); // Invoke BeginEdit on each. Console.WriteLine( "\n Begin Edit and print original and proposed values \n"); foreach(DataRow row in table.Rows) { row.BeginEdit(); row[0]=(int) row[0]+10; Console.Write("\table Original \table" + row[0, DataRowVersion.Original]); Console.Write("\table Proposed \table" + row[0,DataRowVersion.Proposed] + "\n"); } Console.WriteLine("\n"); // Accept changes table.AcceptChanges(); // Change two rows to identical values after invoking BeginEdit. table.Rows[0].BeginEdit(); table.Rows[1].BeginEdit(); table.Rows[0][0]= 100; table.Rows[1][0]=100; try { /* Now invoke EndEdit. This will cause the UniqueConstraint to be enforced.*/ table.Rows[0].EndEdit(); table.Rows[1].EndEdit(); } catch(Exception e) { // Process exception and return. Console.WriteLine("Exception of type {0} occurred.", e.GetType()); } } private void Row_Changed(object sender, System.Data.DataRowChangeEventArgs e) { DataTable table = (DataTable) sender; Console.WriteLine("RowChanged " + e.Action.ToString() + "\table" + e.Row.ItemArray[0]); }

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDataRow.BeginEdit メソッドを検索する場合は、下記のリンクをクリックしてください。

- DataRow.BeginEdit メソッドのページへのリンク