The dbicList control comes with built-in sort functionality for individual or a combination of columns. The sort functionality can be enabled by setting the sortable property of the column to true (allows for sorting when a user clicks on the header) or by using the dbicList.Sort method. An important feature of the dbicList control is its implementation of a collection-based architecture used to manage items. Using the items collection in the control along with the built-in sort functionality, the developer can quickly code advanced iterative search routines for any kind of data type. The following example illustrates how to implement a soft search from a string typed in to a text box for a value in a column (string data type) in the dbicList control. NOTE: The Items collection in the control are searched using a For Each structure to iterate through the collection.
Example: Search the first column in the dbicList control for a match as the text in a text box changes.
VB.NET
Private Sub TextBoxSearchText_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
TextBoxSearchText.TextChanged
'This event fires every time the user changes the text in the textbox edit control.
'NOTE: This provides the effect of searching as the user types the value.
Dim foundItem As Dbi.WinControl.dbiNodeItem
Dim curItem As Dbi.WinControl.dbiNodeItem
'Set the sort direction to ascend in case user input (clicking on a header) has changed it.
Me.dbicListSearchSort.SortDirection = Dbi.WinControl.enumSortDirection.Ascend
'If the items are not sorted appropriately for the search, run the sort first.
If Me.dbicListSearchSort.SortColumn <> 0 Then
'Sort the items on the appropriate column.
'NOTE: Although the control stores the values in a text string,
'the sort routines are based on the Column Type, and sorts the values accordingly.
Me.dbicListSearchSort.Sort(0)
End If
foundItem = Nothing 'Initialize the nodeItem to return
'Iterate through the items collection testing the value in the first column against the textbox value
For Each curItem In Me.dbicListSearchSort.Items
'Use the GetCellText method of the dbiNodeItem to return the value of the text in the
‘column being searched.
If curItem.GetCellText(0).ToUpper >= Me.TextBoxSearchText.Text.ToUpper Then
'If the cell value is greater than or equal to the value being searched,
'stop the search and return the nodeItem.
foundItem = curItem
Exit For
End If
Next
If foundItem Is Nothing Then 'If no node item is found
Beep() 'The FindItem function reached the end of the list
'Select the last item in the list
foundItem = Me.dbicListSearchSort.Items(Me.dbicListSearchSort.Items.Count - 1)
End If
'Set the selected property of the found item to true
foundItem.Selected = True
'Ensure the selected item is visible in the control's viewing area Me.dbicListSearchSort.EnsureVisible(foundItem)
End Sub
C#
private void TextBoxSearchText_TextChanged(object sender, EventArgs e)
{
// This event fires every time the user changes the text in the textbox edit control.
// NOTE: This provides the effect of searching as the user types the value.
Dbi.WinControl.dbiNodeItem foundItem = null;
// Set the sort direction to ascend in case user input (clicking on a header) has changed it.
this.dbicListSearchSort.SortDirection = Dbi.WinControl.enumSortDirection.Ascend;
// If the items are not sorted appropriately for the search, run the sort first.
if (this.dbicListSearchSort.SortColumn != 0)
{
// Sort the items on the appropriate column.
// NOTE: Although the control stores the values in a text string,
// the sort routines are based on the Column Type, and sorts the values accordingly.
this.dbicListSearchSort.Sort(0);
}
// Iterate through the items collection testing the value in the first column
// against the textbox value
foreach (Dbi.WinControl.dbiNodeItem curItem in this.dbicListSearchSort.Items)
{
// Use the GetCellText method of the dbiNodeItem to return the value of the text in
// the column being searched.
// Use the string.CompareOrdinal method to compare the value in the dbiNodeItem cell
// to the value in the textbox.text field.
// NOTE: If the text in the dbiNodeItem cell is less than the text being searched for
// the string.CompareOrdinal() method returns a negative. If they are equal the
// string.CompareOrdinal() returns 0.
if (string.CompareOrdinal(curItem.GetCellText(0).ToUpper(), _
this.TextBoxSearchText.Text.ToUpper()) >= 0)
// If the cell value starts with the user entered text then the results will get highlighted,
// stop the search and return the nodeItem.
{
foundItem = curItem;
break;
}
}
if (foundItem == null) // If no node item is returned
{
// The FindItem function reached the end of the list
Console.Beep();
// Select the last item in the list
foundItem = this.dbicListSearchSort.Items[this.dbicListSearchSort.Items.Count - 1];
}
// Set the selected property of the found item to true
foundItem.Selected = true;
// Ensure the selected item is visible in the control's viewing area
this.dbicListSearchSort.EnsureVisible(foundItem);
}