Print Page | Close Window

Binding to a DevExpress ListBox

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=999
Printed Date: 10-Jun-2026 at 7:09pm


Topic: Binding to a DevExpress ListBox
Posted By: Mark
Subject: Binding to a DevExpress ListBox
Date Posted: 11-Nov-2008 at 4:20pm
I'm in the process of swapping out our System.Windows.Forms.ListBox controls with DevExpress.XtraEditors.ListBoxControl controls and I've ran into a snag with the databinding. From what I can tell there is no IdeaBlade data binder for the DevExpress ListBoxControl. I searched through the forums and couldn't find any information so I'm writing to ask for advice.
 
The form I'm working with has two list box controls on it with some buttons in between that allow the user to move items from one list box to the other. The simplified code for databinding looks like this:
private void doBinding() {
 DevExpress.XtraEditors.ListBoxControl availableFieldsListBox =
  new DevExpress.XtraEditors.ListBoxControl();
 DevExpress.XtraEditors.ListBoxControl fieldsToClearListBox =
  new DevExpress.XtraEditors.ListBoxControl();
 BindableList<SomeObject.SomeField>() arrLeft = new BindableList<SomeObject.SomeField>();
 BindableList<SomeObject.SomeField>() arrRight = new BindableList<SomeObject.SomeField>();
 BindableList<SomeObject.SomeField> fieldList = new BindableList<SomeObject.SomeField>();
 ControlBindingManager ctrlBM = new ControlBindingManager(fieldList);
 ctrlBM.Descriptors.Add(availableFieldsListBox, "Description",
  fieldListConverter(fieldList, arrLeft));
 ctrlBM.Descriptors.Add(fieldsToClearListBox, "Description",
  fieldListConverter(fieldList, arrRight));
}
private ListConverter fieldListConverter(BindableList<SomeObject.SomeField> pDatasource,
  BindableList<SomeObject.SomeField> pArr) {
 ListConverter listConverter = new ListConverter(pDatasource, Editability.Optional,
  true, "Description", "Code");
 listConverter.SetList(pArr);
 return listConverter;
}
The application compiles fine, but when I run it I get the following exception:
 
System.NotSupportedException: Could not locate a suitable binder for a ListBoxControl. Are you missing an assembly reference?
 
All of our other DevExpress controls bind without errors. So i'm wondering, what do I have to do to get this to work like it did with the Windows.Forms listbox, or is that possible?
 
Thanks,
Mark Harmon



Replies:
Posted By: davidklitzke
Date Posted: 12-Nov-2008 at 10:09am
Mark.
 
You are correct.  There is no DataBinder for the DevEx ListBox control.
 
Here are some of your options:
 
(1) Use a .NET ListBox
 
(2) Write the DataBinder yourself.  If you have a Professional license, you might be able to start with the source for the .NET List Box Data Binder, and modify the source.  This might be very easy or very difficult depending upon the complexity of the DevEx Databinding.


Posted By: Mark
Date Posted: 12-Nov-2008 at 10:21am
That's what I needed to know. Thank you David.


Posted By: Mark
Date Posted: 12-Nov-2008 at 12:48pm
Just to follow up on this, I wrote a DataBinder using the source of ListBoxDataBinder.cs as a starting point. I only had to change a few lines of code to get it working. Thanks again.


Posted By: erturkcevik
Date Posted: 14-Nov-2008 at 6:20am
Hi;
Please send me example code, I want know, how working DataBinder using the source of ListBoxDataBinder.cs
 
Best Regards
 


Posted By: Mark
Date Posted: 18-Nov-2008 at 11:34am
Erturkcevik,
 
Here is an example of how to write a DataBinder for a DevExpress ListBoxControl. Use at your own risk. The actual data binding code can get complicated depending on the objects being bound and what the list box does, so I left that out.
 
  public class DevExListBoxDataBinder : ControlDataBinder {
    public override Type ControlType {
      get { return typeof(BaseListBoxControl); }
    }
    protected override double GetBaseFitness(IDataConverter pConverter) {
      return pConverter is ListConverter ? UIFitness.ModeratelyHigh : UIFitness.None;
    }
    public override String BindingPropertyName {
      get { return "SelectedValue"; }
    }
    public override void InitializeControl(Control pControl, ViewDescriptor pDescriptor) {
      base.InitializeControl(pControl, pDescriptor);
      ListConverter converter = pDescriptor.DataConverter as ListConverter;
      BaseListBoxControl control = pControl as BaseListBoxControl;
      // Sorting (if needed) is performed on the list ahead of time
      control.DataSource = converter.List;
      control.DisplayMember = converter.DisplayMember;
      control.ValueMember = converter.ValueMember;
    }
  }
 
 // Somewhere during application initialization
 void AppInit() {
  ...
  ...
  ControlBinderMap.Instance.UpdateMap(typeof(DevExListBoxDataBinder));
  ...
  ...
 }
 
Best wishes,
Mark



Print Page | Close Window