![]() |
Data Analysis: Sorting Records |
|
If you create a table that includes an identity column and that column is the first (or the most left) column of the table, when the user performs data entry, the records are arranged in the incremental order of the numbers of that identity column. Here is an example:
If you want, you can allow the user to arrange the list of records based on another column. This other column would be used as reference. Rearrange the list of records is referred to as sorting and there are two options: ascending and descending. When a list is sorted in ascending order:
All of the techniques used to sort records on a table can also be applied to a query that displays in Datasheet View. To create more advanced queries, the SQL allows you to sort a field on a query and use this arrangement as part of the query. To sort a column in ascending order, you can include the ORDER BY clause in your statement. The formula used is: SELECT What FROM TableName ORDER BY ColumnName; The What and the TableName factors are the same as described earlier. The ColumnName placeholder allows you to specify the column used as reference. When this operation is performed, the values under the ColumnName column would be sorted in ascending order. The value or the other columns would be used to correspond to those of this column. Consider the following example:
Notice that the records under the LastName column are sorted in alphabetical order, and the other values in the other columns simply follow this order. If you use the * operator to include all fields, you can order the list based on any of the table's column. By default, records are ordered in ascending order. Nevertheless, the ascending order is controlled using the ASC keyword specified after the column used as basis. Here is an example:
If you create a database application that contains a table and you want the users to be able to sort values of that table, probably the fastest means of supporting this operation is through the DataGrid control. Its columns are already configured to be sorted in ascending or descending order. If you are working in a Windows Forms Application and using a DataView object, to sort records, the DataView class is equipped with the Sort property that is of type String. To specify the sort order to apply to the records, assign a formatted string to this property.
Instead of sorting a list in ascending order, you can rearrange its values in reverse order. This is referred to as descending order. The algorithms used are the same as for ascending, except that they are applied in reverse. If you want to sort records in descending order, use the DESC keyword after the name of the column, in place of ASC. The DESC keyword produces the opposite result to the ASC effect. Here is an example:
Once again, if you are working in a Windows Forms Application and using a DataView object, to sort records, assign an appropriately built string to the DataView.Sort property. |
|
|
![]() |
||||||||||||||||||||||||||||||||
|
| Text | (Name) |
| Filter By Selection | mnuFiltBySel |
| Filter Excluding Selection | mnuFiltExclSel |
| - | |
| Sort Ascending | mnuSortAsc |
| Sort Descending | mnuSortDesc |
| - | |
| Remove Filter/Sort | mnuRemFiltSort |
Dim colSelected As DataColumn
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tblStudents As DataTable = DsROSH1.Tables("Students")
Dim colStudents As DataColumnCollection = tblStudents.Columns
Dim i As Integer
For i = 0 To colStudents.Count - 1 Step 1
cboColumns.Items.Add(colStudents(i).ColumnName)
Next
cboColumns.SelectedIndex = 0
cboSortOrder.SelectedIndex = 0
SqlDataAdapter1.Fill(DsROSH1)
colSelected = New DataColumn
End Sub
|
Private Sub dataGrid1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles dataGrid1.MouseDown
Dim hti As DataGrid.HitTestInfo = dataGrid1.HitTest(e.X, e.Y)
Dim curCell As DataGridCell
curCell.RowNumber = hti.Row
curCell.ColumnNumber = hti.Column
dataGrid1.CurrentCell = curCell
colSelected = DsROSH1.Tables("Students").Columns(hti.Column)
End Sub
|
Private Sub cboColumns_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles cboColumns.SelectedIndexChanged
Dim strAscDesc As String = "ASC"
If cboSortOrder.Text = "Descending" Then
strAscDesc = "DESC"
End If
dvwStudents.Sort = cboColumns.Text & " " & strAscDesc
End Sub
|
Private Sub cboSortOrder_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles cboSortOrder.SelectedIndexChanged
Dim strAscDesc As String = "ASC"
If cboColumns.SelectedIndex = -1 Then
Exit Sub
End If
If cboSortOrder.Text = "Descending" Then
strAscDesc = "DESC"
End If
dvwStudents.Sort = cboColumns.Text & " " & strAscDesc
End Sub
|
Private Sub mnuSortAsc_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles mnuSortAsc.Click
dvwStudents.Sort = colSelected.ColumnName & " ASC"
End Sub
|
Private Sub mnuSortDesc_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles mnuSortDesc.Click
dvwStudents.Sort = colSelected.ColumnName & " DESC"
End Sub
|
Private Sub mnuRemFiltSort_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles mnuRemFiltSort.Click
dvwStudents.RowFilter = Nothing
dvwStudents.Sort = "StudentID ASC"
End Sub
|
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles btnClose.Click
End
End Sub
|
|
|
||
| Previous | Copyright © 2005 FunctionX, Inc. | Next |
|
|
||