第一个,这个主要是在一个表里选择一些行。但是,如果选择的依据是行的值为空白,也就是没有值的话,我不知条件怎么写,试了好多种方法,不行,所以最后也没用这个。这个方法的条件好像是模仿SQL里面的写法。
private void GetRowsByFilter() { DataTable table = DataSet1.Tables["Orders"]; // Presuming the DataTable has a column named Date. string expression; expression = "Date > '1/1/00'"; DataRow[] foundRows; // Use the Select method to find all rows matching the filter. foundRows = table.Select(expression); // Print column 0 of each returned row. for(int i = 0; i < foundRows.Length; i ++) { Console.WriteLine(foundRows[i][0]); } }
第二个,主要是用DataRow 的delete方法删除掉某些行,可以和第三个例子做一下对比
private void DemonstrateDeleteRow() { // Create a simple DataTable with two columns and ten rows. DataTable table = new DataTable("table"); DataColumn idColumn = new DataColumn("id", Type.GetType("System.Int32")); idColumn.AutoIncrement=true; DataColumn itemColumn = new DataColumn("item", Type.GetType("System.String")); table.Columns.Add(idColumn); table.Columns.Add(itemColumn); // Add ten rows. DataRow newRow; for(int i = 0; i <10; i++) { newRow = table.NewRow(); newRow["item"] = "Item " + i; table.Rows.Add(newRow); } table.AcceptChanges(); DataRowCollection itemColumns = table.Rows; itemColumns[2].Delete(); itemColumns[5].Delete(); Console.WriteLine(itemColumns[3].RowState.ToString()); // Reject changes on one deletion. itemColumns[3].RejectChanges(); // Change the value of the column so it stands out. itemColumns[3]["item"] = "Deleted, Undeleted, Edited"; // Accept changes on others. table.AcceptChanges(); }
第三个,vb写的例子,我也懒得改了。应用行集合的remove方法删除行。
Private Sub RemoveFoundRow(ByVal table As DataTable) Dim rowCollection As DataRowCollection = table.Rows ' Test to see if the collection contains the value. If rowCollection.Contains(TextBox1.Text) Then Dim foundRow As DataRow = rowCollection.Find(TextBox1.Text) rowCollection.Remove(foundRow) Console.WriteLine("Row Deleted") Else Console.WriteLine("No such row found.") End If End Sub