Now that we have a table with columns, a primary key and data, we can display its content. In this example we'll filter the dataset so that we're only working with the current version of the row.
[VB] Dim CurrRows() As DataRow = workTable.Select(Nothing, Nothing, _ System.Data.DataViewRowState.CurrentRows) Dim list As String = System.String.Empty Console.WriteLine("") Console.WriteLine("Writing current rows for " & workTable.Tablename) Dim RowNo As Integer Dim ColNo As Integer For RowNo = 0 to CurrRows.Count - 1 For ColNo = 0 To workTable.Columns.Count – 1 list = "" list &= workTable.Columns(colNo).ColumnName & " = " & _ CurrRows(RowNo)(ColNo).ToString Console.WriteLine(list) Next Next If CurrRows.Count < 1 Then Console.WriteLine("No CurrentRows Found") [C#] DataRow[] CurrRows = workTable.Select(null, null, System.Data.DataViewRowState.CurrentRows ); string list = System.String.Empty; Console.WriteLine(""); Console.WriteLine("Writing Current rows for " + workTable.TableName ); for (int RowNo = 0; RowNo <= CurrRows.Count -1; RowNo++) { for (int ColNo = 0; ColNo <= workTable.Columns.Count - 1; ColNo++) { list = System.String.Empty; list += workTable.Columns[ColNo].ColumnName + " = " + CurrRows[RowNo][ColNo].ToString(); Console.WriteLine(list); } } if (CurrRows.Count < 1 ) { Console.WriteLine("No Current Rows Found"); }
In the code above, we introduce the DataTable class's Select method. The method enables you to retrieve a set of rows that match specified criteria. The method returns an array of DataRow objects.
In the example the method retrieves all of the "Current" rows. What is a "Current" row? The DataTable maintains three versions of each row: Original, Current and Proposed. An original row is the version of the row as it was first added to the table.
The current version of the row holds any changes that have been made to the row. If you make a change to a value in a row, the new value is the current value while the value prior to the change is the original value.
The proposed version exists under special circumstances for a short period of time. The proposed version represents the state moving from original to current. A row moves into the proposed state when the system or the user calls BeginEdit on the row, then edits the row.
[VB] workRow.BeginEdit ' Make some changes to the row. If ValidateColValue(proposedValue) workRow.EndEdit Else workRow.CancelEdit End If [C#] workRow.BeginEdit // make some changes to the row if (ValidateColValue(proposedValue)) workRow.EndEdit; else workRow.CancelEdit;
It is during the "proposed" row state that you can apply validation logic to individual columns by responding to the DataTable object's ColumnChange event. The table passes a reference to the changing column and the proposed value in the ColumnChange event. The user can modify the proposed value or throw an exception to cancel the edit. The row moves out of the proposed state when EndEdit is called. You can also cancel any edits while the row is in a proposed state by calling CancelEdit.