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.
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.
Comments