Description:
I have a data bound form with editors and the data navigator. This is a Contact details form. I'd like to add an edit box to allow quick searches of the records by some field to be carried out, e.g. for a person's name. What is the solution you recommend?
Answer:
You should use an unbound editor to implement an incremental search feature. You may wish to utilize our LookUpEdit editor, because it already has the necessary search functionality.
The LookUp editor may be configured as shown below. The QueryPopUp event is used to prevent the dropdown list from being displayed. However, you may wish to enable it and use the AutoFilter search mode for the LookUp.
C#using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
void SetupSearchHelper(IList data, string member) {
lookUpEdit1.Properties.DataSource = data;
lookUpEdit1.Properties.DisplayMember = member;
lookUpEdit1.Properties.SearchMode = SearchMode.AutoComplete;
lookUpEdit1.Properties.Buttons.Clear();
lookUpEdit1.Properties.NullText = "[Type a contact to find]";
lookUpEdit1.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True;
lookUpEdit1.Properties.TextEditStyle = TextEditStyles.Standard;
}
private void lookUpEdit1_QueryPopUp(object sender, System.ComponentModel.CancelEventArgs e) {
e.Cancel = true;
}
Please notice that the ValueMember property is left blank. In this case, the EditValue corresponds to a row object in the lookup data source.
The EditValueChanged event is used to set the current position in the data list.
C#private void lookUpEdit1_EditValueChanged(object sender, System.EventArgs e) {
LookUpEdit edit = sender as LookUpEdit;
IList data = edit.Properties.DataSource as IList;
BindingManagerBase bm = BindingContext[data];
if(edit.EditValue == null)
bm.Position = 0;
else {
int index = data.IndexOf(edit.EditValue);
if(index > -1)
bm.Position = index;
}
}
Visual BasicPrivate Sub LookUpEdit1_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LookUpEdit1.EditValueChanged
Dim edit As DevExpress.XtraEditors.LookUpEdit = CType(sender, DevExpress.XtraEditors.LookUpEdit)
Dim data As IList = TryCast(edit.Properties.DataSource, IList)
Dim bm As BindingManagerBase = BindingContext(data)
If edit.EditValue Is Nothing Then
bm.Position = 0
Else
Dim index As Integer = data.IndexOf(edit.EditValue)
If index > -1 Then
bm.Position = index
End If
End If
End Sub
See Also:
Why is the lookup table not scrolled, when I select a row in the dropdown list?
How to obtain the selected row from the LookUp editor
How to obtain a value for a specific column within the LookupEdit which corresponds to the currently selected row
How to clear the currently selected value in the LookUp editor