Unable to bind ComboBox SelectedValue property with object property

Ankush Madankar

I am tring to bind comboBox SelectedValue property with one of my class property, here is code for it

  ComboBox1.DataBindings.Add("SelectedValue", _bindingClass, "ID",true);
  //Also tried
  ComboBox1.DataBindings.Add("SelectedValue", _bindingClass, "ID");

Class object structure like this:

public class BindingClass
{
    public long ID { get; set; }
    public string Code { get; set; }
    public string Name { get; set; }
}

Here is code for ccomboBox binding

   public static void BindDataSourceWithCombo(ref ComboBox cmb)
    {
        DataTable _tableSource = (new AccessConnectionManager()).GetDataTableBySQLQuery("select ValueID,ValueName from PicklistValues");

        var _dataSource = (from DataRow _row in _tableSource.Rows
                           select new
                           {
                               ValueMember = _row["ValueID"],
                               DisplayMember = _row["ValueName"].ToString()

                           }).ToList();

        cmb.DataSource = _dataSource;
        cmb.ValueMember = "ValueMember";
        cmb.DisplayMember = "DisplayMember";
    }

ComboBox has ValueMember as int and DisplayMember as string.

But its not working, as soon as I leave comboBox display text set to empty and no value display on comboBox.

EDIT

When I search for DataType of ValueID in Database I found it is Int16 and in class property I was assigning it with Int64. After changing dataType of ID property problem get solve.

LarsTech

Change your class to use an int instead of a long:

public class BindingClass
{
  public int ID { get; set; }
  public string Code { get; set; }
  public string Name { get; set; }
}

Your anonymous linq is most likely interpreting the value as an integer, not a long, and it's interfering with the DataBinding engine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Silverlight combobox selectedvalue string property binding

From Dev

WPF SelectedValue of combobox not updating from viewmodel property

From Dev

How to bind ComboBox to ObservableCollection property?

From Dev

Bind to property of object

From Dev

JavaFX: ComboBox using Object property

From Dev

Unable to bind Model Property to SelectListItem

From Dev

Unable to bind selectlistitem to model property

From Dev

WPF Combobox with string Bind to Int property

From Dev

Trying to bind ComboBox SelectedItem to GroupBox Visibility Property

From Dev

Cant bind a property as enum to combobox in wpf mvvm

From Dev

Bind custom object property to BooleanBinding

From Dev

How to bind control to object property?

From Dev

How to bind control to object property?

From Dev

Bind knockoutjs to javascript object property

From Dev

Winforms Binding a ComboBox SelectedItem to an Object Property

From Dev

Add object property value to combobox using linq

From Dev

Unable to bind IsSelected property of TreeViewItem to ViewModel

From Dev

Unable to bind IsSelected property of TreeViewItem to ViewModel

From Dev

unable to add property to the json object

From Dev

Unable to dynamically increment a property of an object

From Dev

Unable to create an object with array as a property

From Dev

How can I bind to a property from the Selected Item of a ComboBox?

From Dev

How do you bind an enum to a dependency property to a combobox in WPF?

From Dev

How can I bind to a property from the Selected Item of a ComboBox?

From Dev

How to bind a property to a singleton object property from QML

From Dev

AngularJS -- Bind ngModel to an object stored in a property

From Dev

How to bind property on parent object with knockoutjs?

From Dev

How to bind a list which is a property of an object to DataGridView?

From Dev

How to bind template to object in array by property value?

Related Related

  1. 1

    Silverlight combobox selectedvalue string property binding

  2. 2

    WPF SelectedValue of combobox not updating from viewmodel property

  3. 3

    How to bind ComboBox to ObservableCollection property?

  4. 4

    Bind to property of object

  5. 5

    JavaFX: ComboBox using Object property

  6. 6

    Unable to bind Model Property to SelectListItem

  7. 7

    Unable to bind selectlistitem to model property

  8. 8

    WPF Combobox with string Bind to Int property

  9. 9

    Trying to bind ComboBox SelectedItem to GroupBox Visibility Property

  10. 10

    Cant bind a property as enum to combobox in wpf mvvm

  11. 11

    Bind custom object property to BooleanBinding

  12. 12

    How to bind control to object property?

  13. 13

    How to bind control to object property?

  14. 14

    Bind knockoutjs to javascript object property

  15. 15

    Winforms Binding a ComboBox SelectedItem to an Object Property

  16. 16

    Add object property value to combobox using linq

  17. 17

    Unable to bind IsSelected property of TreeViewItem to ViewModel

  18. 18

    Unable to bind IsSelected property of TreeViewItem to ViewModel

  19. 19

    unable to add property to the json object

  20. 20

    Unable to dynamically increment a property of an object

  21. 21

    Unable to create an object with array as a property

  22. 22

    How can I bind to a property from the Selected Item of a ComboBox?

  23. 23

    How do you bind an enum to a dependency property to a combobox in WPF?

  24. 24

    How can I bind to a property from the Selected Item of a ComboBox?

  25. 25

    How to bind a property to a singleton object property from QML

  26. 26

    AngularJS -- Bind ngModel to an object stored in a property

  27. 27

    How to bind property on parent object with knockoutjs?

  28. 28

    How to bind a list which is a property of an object to DataGridView?

  29. 29

    How to bind template to object in array by property value?

HotTag

Archive