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. |
Practical Learning: Sorting Records
|
Text | (Name) |
Filter By Selection | mnuFiltBySel |
Filter Excluding Selection | mnuFiltExclSel |
- | |
Sort Ascending | mnuSortAsc |
Sort Descending | mnuSortDesc |
- | |
Remove Filter/Sort | mnuRemFiltSort |
private: /// <summary> /// Required designer variable. /// </summary> System::ComponentModel::Container * components; DataColumn *colSelected; |
System::Void Form1_Load(System::Object * sender, System::EventArgs * e) { DataTable *tblStudents = this->dsROSH1->Tables->Item[S"Students"]; DataColumnCollection *colStudents = tblStudents->Columns; for(int i = 0; i < colStudents->Count; i++) this->cboColumns->Items->Add(colStudents->Item[i]->ColumnName); this->cboColumns->SelectedIndex = 0; this->cboSortOrder->SelectedIndex = 0; this->sqlDataAdapter1->Fill(this->dsROSH1); colSelected = new DataColumn; } |
System::Void dataGrid1_MouseDown(System::Object * sender, System::Windows::Forms::MouseEventArgs * e) { DataGrid::HitTestInfo *hti = this->dataGrid1->HitTest(e->X, e->Y); DataGridCell curCell; curCell.RowNumber = hti->Row; curCell.ColumnNumber = hti->Column; this->dataGrid1->CurrentCell = curCell; colSelected = this->dsROSH1->Tables->Item[S"Students"]->Columns->Item[hti->Column]; } |
System::Void cboColumns_SelectedIndexChanged(System::Object * sender, System::EventArgs * e) { String *strAscDesc = S"ASC"; if( this->cboSortOrder->Text->Equals(S"Descending") ) strAscDesc = S"DESC"; this->dvwStudents->Sort = String::Concat(this->cboColumns->Text, S" ", strAscDesc); } |
System::Void cboSortOrder_SelectedIndexChanged(System::Object * sender, System::EventArgs * e) { String *strAscDesc = S"ASC"; if( this->cboColumns->SelectedIndex == -1 ) return; if( this->cboSortOrder->Text->Equals(S"Descending") ) strAscDesc = S"DESC"; this->dvwStudents->Sort = String::Concat(this->cboColumns->Text, S" ", strAscDesc); } |
System::Void mnuSortAsc_Click(System::Object * sender, System::EventArgs * e) { this->dvwStudents->Sort = String::Concat(colSelected->ColumnName, S" ASC"); } |
System::Void mnuSortDesc_Click(System::Object * sender, System::EventArgs * e) { this->dvwStudents->Sort = String::Concat(colSelected->ColumnName, S" DESC"); } |
System::Void mnuRemFiltSort_Click(System::Object * sender, System::EventArgs * e) { this->dvwStudents->RowFilter = 0; this->dvwStudents->Sort = String::Concat(S"StudentID", S" ASC"); } |
System::Void btnClose_Click(System::Object * sender, System::EventArgs * e) { Close(); } |
|
||
Previous | Copyright © 2005-2016, FunctionX | Next |
|