Danh mục

Adding, Modifying, and Removing DataRowView Objects from a DataView

Số trang: 7      Loại file: pdf      Dung lượng: 21.22 KB      Lượt xem: 1      Lượt tải: 0    
Thu Hiền

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Adding, Modifying, and Removing DataRowView Objects from a DataView Its important to understand that DataRowView objects in a DataView provide access to the underlying DataRow objects in a DataTable.
Nội dung trích xuất từ tài liệu:
Adding, Modifying, and Removing DataRowView Objects from a DataViewAdding, Modifying, and Removing DataRowView Objects from a DataViewIts important to understand that DataRowView objects in a DataView provide access tothe underlying DataRow objects in a DataTable. Therefore, when you examine and editthe contents of a DataRowView, youre actually working with the underlying DataRow.Similarly, when you remove a DataRowView, you are removing the underlyingDataRow.Adding a DataRowView to a DataViewTo add a new DataRowView to a DataView, you call the AddNew() method of yourDataView. The AddNew() method returns a DataRowView object that you use to set thecolumn values for the new row.The following example calls the AddNew() method of thecustomersDV DataView:DataRowView customerDRV = customersDV.AddNew();customerDRV[CustomerID] = J7COM;customerDRV[CompanyName] = J7 Company;customerDRV[Country] = UK;customerDRV.EndEdit();Notice the use of the EndEdit() method of the customerDRV DataRowView to end theediting. The EndEdit() method creates a new DataRow in the underlying DataTable. TheDataColumn objects in the new DataRow will contain the column values specified in theprevious code.Note You can undo the addition by calling the CancelEdit() method of a DataRowView.You can get the underlying DataRow added to the DataTable using the Row property of aDataRowView. For example:DataRow customerDR = customerDRV.Row;Modifying an Existing DataRowViewTo begin modifying an existing DataRowView in a DataView, you call the BeginEdit()method of the DataRowView in your DataView. The following example calls theBeginEdit() method for the first DataRowView in customersDV:customersDV[0].BeginEdit();Note Remember that DataRowView objects in a DataView start at index 0, and therefore customersDV[0] is the first DataRowView in customersDV.You can then modify a DataColumn in the underlying DataRow through theDataRowView. The following example sets the CompanyName DataColumn to WidgetsInc.:customersDV[0][CompanyName] = Widgets Inc.;Once youve finished making your modifications, you call the EndEdit() method to makeyour modifications permanent in the underlying DataTable. For example:customersDV[0].EndEdit();Note You can undo the modification by calling the CancelEdit() method of a DataRowView.Removing an Existing DataRowViewTo remove an existing DataRowView from a DataView, you can call the Delete() methodof either the DataView or the DataRowView. When calling the Delete() method of aDataView, you pass the index of the DataRowView you want to remove. The followingexample removes the second DataRowView from customersDV:customersDV.Delete(1);When calling the Delete() method of a DataRowView, you simply call that method of theDataRowView in your DataView. The following example removes the thirdDataRowView from customersDV:customersDV[2].Delete();With either of these Delete() methods, the deletion isnt committed in the underlyingDataTable until you call the AcceptChanges() method of your DataTable. For example:customersDT.AcceptChanges();Note You can call the RejectChanges() method of a DataTable to undo the deletions. This method will also undo any uncommitted additions and modifications of rows.Listing 13.3 shows a program that adds, modifies, and removes DataRowView objectsfrom a DataView. This program also displays the IsNew and IsEdit properties of theDataRowView objects, which indicate whether the DataRowView is new and is beingedited.Listing 13.3: ADDMODIFYANDREMOVEDATAROWVIEWS.CS/* AddModifyAndRemoveDataRowViews.cs illustrates how to add, modify, and remove DataRowView objects from a DataView*/using System;using System.Data;using System.Data.SqlClient;class AddModifyAndRemoveDataRowViews{ public static void DisplayDataRow( DataRow myDataRow, DataTable myDataTable ) { Console.WriteLine(\nIn DisplayDataRow()); foreach (DataColumn myDataColumn in myDataTable.Columns) { Console.WriteLine(myDataColumn + = + myDataRow[myDataColumn]); }}public static void Main(){ SqlConnection mySqlConnection = new SqlConnection( server=localhost;database=Northwind;uid=sa;pwd=sa ); SqlCommand mySqlCommand = mySqlConnection.CreateCommand(); mySqlCommand.CommandText = SELECT CustomerID, CompanyName, Country + FROM Customers; SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); mySqlDataAdapter.SelectCommand = mySqlCommand; DataSet myDataSet = new DataSet(); mySqlConnection.Open(); mySqlDataAdapter.Fill(myDataSet, Customers); mySqlConnection.Close(); DataTable customersDT = myDataSet.Tables[Customers];// set up the filter expressionstring filterExpression = Country = UK;// create a DataView object named customersDVDataView customersDV = new DataView();customersDV.Table = customersDT;customersDV.RowFilter = filterExpression;// add a new DataRowView (adds a DataRow to the DataTable)Console.WriteLine(\nCalling customersDV.AddNew());DataRowView customerDRV = customersDV.AddNew();customerDRV[CustomerID] = J7COM;customerDRV[CompanyName] = J7 Company;customerDRV[Country] = UK;Console.WriteLine(customerDRV[\ CustomerID\] = + customerDRV[CustomerID]);Console.WriteLine(customerDRV[\ CompanyName\] = + customerDRV[CompanyName]);Console.WriteLine(customerDRV[\ Country\] = + customerDRV[Country]);Console.WriteLine(customerDRV.IsNew = + customerDRV.IsNew);Console.WriteLine(customerDRV.IsEdit = + customerDRV.IsEdit);customerDRV.EndEdit();// get and display the underlying DataRowDataRow customerDR = customerDRV.Row;DisplayDataRow(customerDR, customersDT);// modify the CompanyName of customerDRVConsole.WriteLine(\nSetting customersDV[0][\ CompanyName\] to Widgets Inc.);customersDV[0].BeginEdit();customersDV[0][CompanyName] = Widgets Inc.;Console.WriteLine(customersDV[0][\ CustomerID\] = + ...

Tài liệu được xem nhiều: