The DataView: Filtering, Adding, Editing, Sorting, and Deleting DataRowViews

DataView: Filtering, Adding, Editing, Sorting, and Deleting DataRowViews "-->

only for RuBoard

The DataView : Filtering, Adding, Editing, Sorting, and Deleting DataRowViews

As previously mentioned, a DataView is a customized view of a DataTable . Essentially, when you create a DataView you are taking a snapshot of the DataTable and then manipulating the data some how ”whether it be sorting, filtering, adding, etc. For instance, you can create two DataView objects from one DataTable and filter out all rows where the primary key value starts with "A" in one of them and in the second you can filter out all rows where the primary key value starts with "B". Each of the two DataView objects is a separate entity to a degree, but they are still dependent and bound to the DataTable from which they were both derived.

A DataView can be created using an existing DataTable . The following code creates a new DataView :

 [VisualBasic.NET] - dim dv as new DataView(MyDataTable) [C#.NET] -  DataView dv = new DataView(MyDataTable); 

We have included Listing 11.6 so you can get a good feel for how a DataView works before we jump into some of its more rich features such as editing, sorting, and filtering. Listing 11.6 illustrates how to create multiple DataView objects from one DataTable object and binds each to a separate DataGrid .

Listing 11.6 Creating Two DataViews from the Same DataTable
 [VisualBasic.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: 04: <script language="vb" runat="server"> 05: 06:  protected sub Page_Load(sender as Object, e as EventArgs) 07: 08:   dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 09:   dim SqlDA as new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID FROM graphics/ccc.gif Products", SqlCon) 10:   dim ds as new DataSet() 11: 12:   SqlDA.Fill(ds, "Products") 13: 14:   dim dt as DataTable = ds.Tables(0) 15: 16:   dim MyDataView1 as new DataView(dt) 17:   MyDataView1.Sort = "ProductName ASC" 'Default 18: 19:   dim MyDataView2 as new DataView(dt) 20:   MyDataView2.Sort = "ProductName DESC" 21: 22:   DG.DataSource = MyDataView1 23:   DG.DataBind() 24: 25:   DG2.DataSource = MyDataView2 26:   DG2.DataBind() 27: 28:  end sub 29: 30: </script> [C#.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: 04: <script language="C#" runat="server"> 05: 06:  protected void Page_Load(Object sender, EventArgs e){ 07: 08:   SqlConnection SqlCon = new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind"); 09:   SqlDataAdapter SqlDA = new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID graphics/ccc.gif FROM Products", SqlCon); 10:   DataSet ds = new DataSet(); 11: 12:   SqlDA.Fill(ds, "Products"); 13: 14:   DataTable dt = ds.Tables[0]; 15: 16:   DataView MyDataView1 = new DataView(dt); 17:   MyDataView1.Sort = "ProductName ASC"; //Default 18: 19:   DataView MyDataView2 = new DataView(dt); 20:   MyDataView2.Sort = "ProductName DESC"; 21: 22:   DG.DataSource = MyDataView1; 23:   DG.DataBind(); 24: 25:   DG2.DataSource = MyDataView2; 26:   DG2.DataBind(); 27: 28:  } 29: 30: </script> [VisualBasic.NET & C#.NET] 31: <html> 32:  <body> 33: 34:   <h3>DataGrid - Ordered ASC by ProductName</h3> 35:   <asp:DataGrid id="DG" runat="Server" font-size="10" /> 36: 37:   <br> 38: 39:   <h3>DataGrid - Ordered DESC by ProductName</h3> 40:   <asp:DataGrid id="DG2" runat="Server" font-size="10" /> 41: 42:  </body> 43: </html> 

In Listing 11.6, two DataView objects are created from the same DataTable . The first DataView created is on line 16 and named MyDataView1 . The rows in MyDataView1 are sorted in ascending order by the ProductName value (line 17), and is the DataGrid named DG DataSource . The second DataView is created on line 19 and is named MyDataView2 . MyDataView2 is sorted in descending order by the ProductName column value (line 20), and is the DataGrid named DG2 DataSource . We will be going over the DataView.Sort attribute later in this section. When this page loads, there will be two different DataGrids displayed. The top DataGrids contents are sorted in alphabetical order, and the bottom is in reverse alphabetical order, as seen in Figure 11.6.

Figure 11.6. The top DataGrid contents are in alphabetical order and the bottom in reverse alphabetical order.
graphics/11fig06.gif

Now that you have a basic understanding of how to create and use a DataView , let's move on to some of its attributes you can use to manipulate its DataRowCollection .

Filtering Rows in a DataView

You can filter rows in a DataView by using the DataView.RowFilter property. The syntax for the value of this property is a column name followed by an operator (for example: < , > , = ) and ending in a value to filter on ”for more information on filter expressions see the DataColumn.Expression property. The DataView.RowFilter is similar to the DataTable.Select( FilterExpression ) method and if given access to the same DataRowCollection both will contain the same rows if the same filterexpression is used. The difference is how the rows are returned: In the DataTable.Select method an array of DataRows is returned while the DataView.RowFilter merely affects what rows are in the DataView .

Listing 11.7 contains an example using the DataView.RowFilter attribute. In this example, I again use two different DataView objects: The first DataView will be filtered to just contain rows where the ProductName column value starts with "A" and the second DataView will only contain rows where the ProductName column value starts with "B".

Listing 11.7 Using the DataView.RowFilter Attribute
 [VisualBasic.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: 04: <script language="vb" runat="server"> 05: 06:  protected sub Page_Load(sender as Object, e as EventArgs) 07: 08:   dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 09:   dim SqlDA as new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID FROM graphics/ccc.gif Products", SqlCon) 10:   dim ds as new DataSet() 11: 12:   SqlDA.Fill(ds, "Products") 13: 14:   dim dt as DataTable = ds.Tables(0) 15: 16:   dim  MyDataView1 as new DataView(dt) 17:   MyDataView1.RowFilter = "ProductName LIKE 'A%'" 18: 19:   dim MyDataView2 as new DataView(dt) 20:   MyDataView2.RowFilter = "ProductName LIKE 'B%'" 21: 22:   DG.DataSource = MyDataView1 23:   DG.DataBind() 24: 25:   DG2.DataSource = MyDataView2 26:   DG2.DataBind() 27: 28:  end sub 29: 30: </script> [C#.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: 04: <script language="C#" runat="server"> 05: 06:  protected void Page_Load(Object sender, EventArgs e){ 07: 08:   SqlConnection SqlCon = new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind"); 09:   SqlDataAdapter SqlDA = new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID graphics/ccc.gif FROM Products", SqlCon); 10:   DataSet ds = new DataSet(); 11: 12:   SqlDA.Fill(ds, "Products"); 13: 14:   DataTable dt = ds.Tables[0]; 15: 16:   DataView MyDataView1 = new DataView(dt); 17:   MyDataView1.RowFilter = "ProductName LIKE 'A%'"; 18: 19:   DataView MyDataView2 = new DataView(dt); 20:   MyDataView2.RowFilter = "ProductName LIKE 'B%'"; 21: 22:   DG.DataSource = MyDataView1; 23:   DG.DataBind(); 24: 25:   DG2.DataSource = MyDataView2; 26:   DG2.DataBind(); 27: 28:  } 29: 30: </script> [VisualBasic.NET & C#.NET] 01: <html> 02:  <body> 03: 04:   <h3>ProductName Starts With A</h3> 05:   <asp:DataGrid id="DG" runat="Server" font-size="10" /> 06: 07:   <br> 08: 09:   <h3>ProductName Starts With B</h3> 10:   <asp:DataGrid id="DG2" runat="Server" font-size="10" /> 11: 12:  </body> 13: </html> 

In Listing 11.7, I use the DataView.DataRowFilter to filter all rows that start with the letter "A" (line 17) for the DataView named MyDataView1 and on line 20, I use the DataView.DataRowFilter to filter rows that start with the letter "B". Figure 11.7 contains an illustration of this page.

Figure 11.7. The top DataGrid contains all rows where the ProductName value starts with "A", and the bottom DataGrid contains just rows that start with "B".
graphics/11fig07.gif

Recall that you can use the DataTable.Select method's third parameter to filter rows based on their state ”you also can do this using the DataView by setting the DataView.RowStateFilter property. This functionality is exactly the same as the DataTables . The following list contains all the possible values. These values can be found in the DataViewRowState enumeration.

  • CurrentRows ” The current rows

  • Deleted ” All deleted rows

  • ModifiedCurrent ” The current version of modified data

  • ModifiedOriginal ” The original version of modified data

  • Added ” A new row

  • None ” None

  • OriginalRows ” The original rows

  • Unchanged ” Unchanged rows

In the following code, you'll find an example of implementing this type of filtering:

 [Visual Basic] - DataView.RowStateFilter = DataViewRowState.Added [C#] - DataView.RowStateFilter = DataViewRowState.Added; 

You can optionally filter with more than one DataViewRowState enumerator by using the OR Boolean operator ( C# = )( VB = OR ) as seen in the following.

 [Visual Basic] - DataView.RowStateFilter =  DataViewRowState.New OR DataViewRowState.Deleted [C#] - DataView.RowStateFilter =  DataViewRowState.New  DataViewRowState.Deleted; 

Adding a New Row to a DataView

Adding a new row to a DataView is much like adding a new row to the DataTable except that you use a DataRowView object instead of a DataRow object, and the DataView method you invoke to return the DataRowView is slightly different. There is one method and one property I want to introduce to you before we get to the code example:

  • DataView.AllowNew ” This property expects a boolean value indicating whether or not this DataView can have new rows added.

  • DataView.AddNew ” This method adds a new row to the DataView and returns it so you can give the different fields values.

Listing 11.8 shows how to add a new row to a DataView . In this example, you are going to add one new row to a DataView and then bind it to a DataGrid .

Listing 11.8 Adding a New DataRowView
 [VisualBasic.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: <script language="vb" runat="server"> 04: 05:  protected sub Page_Load(sender as Object, e as EventArgs) 06: 07:   dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 08:   dim SqlDA as new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID FROM graphics/ccc.gif Products", SqlCon) 09:   dim ds as new DataSet() 10: 11:   SqlDA.Fill(ds, "Products") 12: 13:   dim dt as DataTable = ds.Tables(0) 14: 15:   dim MyDataView1 as new DataView(dt) 16:   MyDataView1.AllowNew = true 17:   dim MyDataRowView as DataRowView = MyDataView1.AddNew() 18:   MyDataRowView("ProductName") = "An Dim Sum" 19:   MyDataRowView("ProductID") = "12" 20: 21:   DG.DataSource = MyDataView1 22:   DG.DataBind() 23: 24:  end sub 25: 26: </script> [C#.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: <script language="C#" runat="server"> 04: 05:  protected void Page_Load(Object sender, EventArgs e){ 06: 07:   SqlConnection SqlCon = new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind"); 08:   SqlDataAdapter SqlDA = new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID graphics/ccc.gif FROM Products", SqlCon); 09:   DataSet ds = new DataSet(); 10: 11:   SqlDA.Fill(ds, "Products"); 12: 13:   DataTable dt = ds.Tables[0]; 14: 15:   DataView MyDataView1 = new DataView(dt); 16:   MyDataView1.AllowNew = true; 17:   DataRowView MyDataRowView = MyDataView1.AddNew(); 18:   MyDataRowView["ProductName"] = "An Dim Sum"; 19:   MyDataRowView["ProductID"] = "12"; 20: 21:   DG.DataSource = MyDataView1; 22:   DG.DataBind(); 23: 24:  } </script> [VisualBasic.NET & C#.NET] 25: <html> 26:  <body> 27:   <h3> 28:    Adding a new DataRowView 29:    </h3> 30:    <asp:DataGrid id="DG" runat="Server" font-size="10" /> 31:  </body> 32: </html> 

Listing 11.8 might look famililar to you. It is very similar to adding a new DataRow to the DataTable except when using the DataTable you use the DataTable.NewRow method. In a DataView , you use the DataView.AddNew method to add the new row ”line 17. On the line before the DataView.AddNew method is invoked, I set the DataView.AllowNew property to true . This indicates that new rows can be added to the DataView . Finally, on lines 18 and 19, I apply values to both fields from the DataView ProductName and ProductID and then I bind the DataGrid to the DataView . If I wanted to add more than one row to the DataView , I would have to invoke the DataView.AddNew method again after applying the values to the first DataRowView added.

Caution

When you call the DataView.AddNew method, a new DataRowView is added regardless of whether you apply values to the new row. So if you call the DataView.AddNew method and don't apply values, an empty row will be in the DataView .


You can see this page in Figure 11.8. You will see the newly added product at the very bottom of the DataGrid .

Figure 11.8. A Dim Sum can be seen at the very bottom of the DataGrid .
graphics/11fig08.gif

Note

Adding a new row to a DataView doesn't affect either the DataTable from which it was derived or any other DataView objects that were derived from the DataTable .


Editing Rows in a DataView

Editing using the DataView object is very similar to editing in the DataTable object. You must determine which row or rows you want to edit within the DataRowCollection , make sure the DataView is indeed editable, and finally edit the DataRowView .

There are a couple of properties and methods with which to be concerned when editing a row in a DataView . The following list goes over each. Note that some are DataView properties or methods and some are DataRowViews .

  • DataView.AllowEdit ” This boolean property enables you to get or set whether or not edits are allowed.

  • DataRowView.BeginEdit ” The method that puts the DataRowView into edit mode.

  • DataRowView.EndEdit ” The method that takes the DataRowView out of edit mode.

  • DataRowView.CancelEdit ” Cancels and rolls back any edits made on the DataRowView .

Now let's look at a code example ”Listing 11.9 contains an example editing a DataRowView .

Listing 11.9 Editing a DataRowView
 [VisualBasic.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: <script language="vb" runat="server"> 04: 05:  protected sub Page_Load(sender as Object, e as EventArgs) 06: 07:   dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 08:   dim SqlDA as new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID FROM graphics/ccc.gif Products", SqlCon) 09:   dim ds as new DataSet() 10: 11:   SqlDA.Fill(ds, "Products") 12: 13:   dim dt as DataTable = ds.Tables(0) 14: 15:   dim MyDataView1 as new DataView(dt) 16:   dim DRV as DataRowView = MyDataView1(0) 17:   MyDataView1.AllowEdit = true 18:   DRV.BeginEdit() 19:   DRV("ProductName") = "New and Improved " + DRV("ProductName") 20:   DRV.EndEdit() 21: 22:   DG.DataSource = MyDataView1 23:   DG.DataBind() 24: 25:  end sub 26: 27: </script> [C#.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: <script language="C#" runat="server"> 04: 05:  protected void Page_Load(Object sender, EventArgs e){ 06: 07:   SqlConnection SqlCon = new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind"); 08:   SqlDataAdapter SqlDA = new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID graphics/ccc.gif FROM Products", SqlCon); 09:   DataSet ds = new DataSet(); 10: 11:   SqlDA.Fill(ds, "Products"); 12: 13:   DataTable dt = ds.Tables[0]; 14: 15:   DataView MyDataView1 = new DataView(dt); 16:   DataRowView DRV = MyDataView1[0]; 17:   MyDataView1.AllowEdit = true; 18:   DRV.BeginEdit(); 19:   DRV["ProductName"] = "New and Improved " + DRV["ProductName"]; 20:   DRV.EndEdit(); 21: 22:   DG.DataSource = MyDataView1; 23:   DG.DataBind(); 24: 25:  } 26: 27: </script> [VisualBasic.NET & C#.NET] 28: <html> 29:  <body> 30:   <h3> 31:    Adding a new DataRowView 32:   </h3> 33:    <asp:DataGrid id="DG" runat="Server" font-size="10" /> 34:  </body> 35: </html> 

In Listing 11.9, I enable editing within the DataView on line 17 by setting the DataView.AllowEdit property. On line 18, I put the first DataRowView in the DataView into edit mode by invoking DataRowView.BeginEdit . I change the value of the ProductName column on line 19, and invoke the DataRowView.EndEdit method on line 20 to save the changes and bind the DataGrid on lines 22 and 23. You can see this page in Figure 11.9.

Figure 11.9. The "Alice Mutton" field is now "New and Improved Alice Mutton."
graphics/11fig09.gif

In Listing 11.9, I took the easy way out and just edited the first row in the DataRowCollection , but you might want to edit the third or three-hundreth row. In the following sections I'll illustrate how you can search a DataView for specific DataRowView's based on their columns values by using the DataView.Find method.

Finding DataRowViews

The DataView.Find method finds a row based on a primary key value and returns an integer value indicating the index of the row where the value was found in the DataRowCollection .

There is an additional property you need to set before using the DataView.Find method, DataView.Sort . The DataView.Sort method gets or sets how the DataView is sorted. The value can be one or more column names followed by an optional ASC or DESC (sort in ascending or descending order). Listing 11.10 illustrates how to use the DataView.Find and DataView.Sort method. In this example, we will use the Find and Sort method to find a specific row, and then we'll edit it and bind to a DataGrid .

Listing 11.10 Using the Find Method
 [VisualBasic.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: <script language="vb" runat="server"> 04: 05:  protected sub Page_Load(sender as Object, e as EventArgs) 06: 07:   dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 08:   dim SqlDA as new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID FROM graphics/ccc.gif Products", SqlCon) 09:   dim ds as new DataSet() 10: 11:   SqlDA.Fill(ds, "Products") 12: 13:   dim dt as DataTable = ds.Tables(0) 14: 15:   dim MyDataView1 as new DataView(dt) 16: 17:   MyDataView1.Sort = "ProductID DESC" 18:   dim row as integer = MyDataView1.Find("17") 19:   MyDataView1.AllowEdit = true 20:   MyDataView1(row).BeginEdit() 21:   MyDataView1(row)("ProductName") = "New and Improved " + graphics/ccc.gif MyDataView1(row)("ProductName") 22:   MyDataView1(row).EndEdit() 23: 24:   DG.DataSource = MyDataView1 25:   DG.DataBind() 26: 27:  end sub 28: 29: </script> [C#.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: <script language="C#" runat="server"> 04: 05:  protected void Page_Load(Object sender, EventArgs e){ 06: 07:   SqlConnection SqlCon = new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind"); 08:   SqlDataAdapter SqlDA = new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID graphics/ccc.gif FROM Products", SqlCon); 09:   DataSet ds = new DataSet(); 10: 11:   SqlDA.Fill(ds, "Products"); 12: 13:   DataTable dt = ds.Tables[0]; 14: 15:   DataView MyDataView1 = new DataView(dt); 16: 17:   MyDataView1.Sort = "ProductID DESC"; 18:   int row = MyDataView1.Find("17"); 19:   MyDataView1.AllowEdit = true; 20:   MyDataView1[row].BeginEdit(); 21:   MyDataView1[row]["ProductName"] = "New and Improved " + graphics/ccc.gif MyDataView1[row]["ProductName"]; 22:   MyDataView1[row].EndEdit(); 23: 24:   DG.DataSource = MyDataView1; 25:   DG.DataBind(); 26: 27:  } 28: 29: </script> [VisualBasic.NET & C#.NET] 28: <html> 29:  <body> 30:   <h3> 31:    Adding a new DataRowView 32:   </h3> 33:    <asp:DataGrid id="DG" runat="Server" font-size="10" /> 34:  </body> 35: </html> 

In Listing 11.10, the first thing I do is sort the DataView , line 17. I sort by the ProductID column in descending order which not only sets the DataView s sort order, but also sets the ProductID column as the column to search when using the DataView.Find method. On line 18, I call the DataView.Find method passing a value of 17, which is "Alice Muttton's" ProductID (Note: Your Alice Mutton ProductID may be different). This method returns an integer value of where this row is located within the DataRowCollection . I put the returned integer in the variable row . I use this integer ( row ) to edit this row in lines 20 “22 and finally bind the DataGrid . Figure 11.10 contains this page. Notice that "Alice Mutton" has been edited to read "New and Improved Alice Mutton".

Figure 11.10. The "Alice Mutton" field is now New and Improved Alice Mutton.
graphics/11fig10.gif

Deleting DataRowViews

There are two ways you can delete a DataRowView from a DataView . The first is by using the DataView.Delete method and the second is by using the DataRowView.Delete method. The following describes each:

  • DataView.Delete ” This method deletes a row at a specified index. DataView.Delete expects one parameter, the index you want to delete.

  • DataRowView.Delete ” Deletes the row that invoked the method.

Listing 11.11 contains an example using both methods. In this example, there are two different DataViews created and the DataRowView with the value " Alice Mutton " will be deleted from both.

Listing 11.11 Deleting a Row in a DataView
 [VisualBasic.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: <script language="vb" runat="server"> 04: 05:  protected sub Page_Load(sender as Object, e as EventArgs) 06: 07:   dim SqlCon as new SqlConnection("server=localhost; uid=sa;pwd=;database=northwind") 08:   dim SqlDA as new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID FROM graphics/ccc.gif Products", SqlCon) 09:   dim ds as new DataSet() 10: 11:   SqlDA.Fill(ds, "Products") 12: 13:   dim dt as DataTable = ds.Tables(0) 14: 15:   dim MyDataView1 as new DataView(dt) 16:   dim MyDataView2 as new DataView(dt) 17: 18:   MyDataView1.Sort = "ProductID DESC" 19:   dim row as integer = MyDataView1.Find("17") 20:   MyDataView1.AllowDelete = true 21:   MyDataView1(row).Delete() 22:   DG.DataSource = MyDataView1 23:   DG.DataBind() 24: 25:   dt.RejectChanges() 'Rollback First Delete 26: 27:   MyDataView2.Sort = "ProductID DESC" 28:   row = MyDataView2.Find("17") 29:   MyDataView2.AllowDelete = true 30:   MyDataView2.Delete(row) 31:   DG2.DataSource = MyDataView2 32:   DG2.DataBind() 33: 34:  end sub 35: 36: </script> [C#.NET] 01: <%@ import namespace="System.Data" %> 02: <%@ import namespace="System.Data.SqlClient" %> 03: <script language="C#" runat="server"> 04: 05:  protected void Page_Load(Object sender, EventArgs e){ 06: 07:   SqlConnection SqlCon = new SqlConnection("server=localhost; graphics/ccc.gif uid=sa;pwd=;database=northwind"); 08:   SqlDataAdapter SqlDA = new SqlDataAdapter("SELECT TOP 10 ProductName, ProductID graphics/ccc.gif FROM Products", SqlCon); 09:   DataSet ds = new DataSet(); 10: 11:   SqlDA.Fill(ds, "Products"); 12: 13:   DataTable dt = ds.Tables[0]; 14: 15:   DataView MyDataView1 = new DataView(dt); 16:   DataView MyDataView2 = new DataView(dt); 17: 18:   MyDataView1.Sort = "ProductID DESC"; 19:   int row = MyDataView1.Find("17"); 20:   MyDataView1.AllowDelete = true; 21:   MyDataView1[row].Delete(); 22:   DG.DataSource = MyDataView1; 23:   DG.DataBind(); 24: 25:   dt.RejectChanges(); //Rollback First Delete 26: 27:   MyDataView2.Sort = "ProductID DESC"; 28:   row = MyDataView2.Find("17"); 29:   MyDataView2.AllowDelete = true; 30:   MyDataView2.Delete(row); 31:   DG2.DataSource = MyDataView2; 32:   DG2.DataBind(); 33: 34:  } 35: 36: </script> [VisualBasic.NET & C#.NET] 37: <html> 38:  <body> 39:   <h3> 40:   DataRowView.Delete() 41:   </h3> 42:   <asp:DataGrid id="DG" runat="Server" font-size="10" /> 43:   <br> 44:   <h3> 45:   DataView.Delete() 46:   </h3> 47:   <asp:DataGrid id="DG2" runat="Server" font-size="10" /> 48:   </body> 49: </html> 

First let's go through the DataRowView.Delete section of Listing 11.11. On line 18, I sort the DataView , then on line 19, I use the DataView.Find method to get the index within the DataRowCollection of "Alice Mutton". On line 20, I set DataView.AllowDelete to true and invoke the DataRowView.Delete method passing in the proper row index on line 21. Lastly, I bind the DataGrid to the MyDataView1 .

You might be wondering why I invoke the DataTable.RejectChanges method on line 25. I do this to roll back the first deletion because when a row is deleted from the DataRowCollection , it affects all objects associated with it ”the parent DataTable and associated DataViews . For instance, if I did not invoke the DataTable.RejectChanges , I would have gotten an exception on line 30 (the DataView.Delete method) because the DataView.Find method would not return an index and I would be passing in an invalid index as a parameter for the DataView.Delete method. Try commenting out line 25 to see the effects.

Let's move on to the DataView.Delete method section of the code. Lines 27 “29 are identical to the first method. First, I sort the rows (line 27), then I find the specific row I want to delete (line 28), and enable deleting for the DataView (line 29). The DataView.Delete method is invoked on line 30 and I use the row variable as a parameter. Finally, I bind the second DataGrid to this DataView . Figure 11.11 contains this page.

Figure 11.11. Alice Mutton is deleted in both DataGrids .
graphics/11fig11.gif

I also would like to suggest trying the following to show the relationship between the DataTable and the DataViews derived from it:

  1. Change the parameter passed into the DataView.Find method on line 28 to a different ProductID value, comment out line 25 and re-execute the page. You will see that the bottom DataGrid will have two different rows deleted.

  2. Leaving the code from number 1 intact, add another DataGrid , but bind this one to the DataTable from which the two DataViews were derived. Put the data binding code after all other code. You will see that both rows are deleted from the DataTable .

only for RuBoard


Programming Data-Driven Web Applications with ASP. NET
Programming Data-Driven Web Applications with ASP.NET
ISBN: 0672321068
EAN: 2147483647
Year: 2000
Pages: 170

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net