0

I'm facing some problem with databinding in C# windows Form project. I used .Net Framework 4 client profile. And IDE is visual studio 2010 express edition for C#. I have a class as below.

public myClass()
    {
        name = string.Empty;
        dataType = DataTypes.Bool;  //property with enum created by me
        isList = false;
        filterValue = new object();
    }

then I have a profile.csv file. In one of my form, I have a combobox and its datasource is datatable which include data from csv file.

private void form_load()
{
 ComboBox comboBox = new ComboBox();
 this.Controls.Add(comboBox);
 comboBox.Name = attribute.Name.Replace(" ", string.Empty);
 comboBox.DisplayMember = attribute.Name;
 comboBox.DataSource = attributeValueDataSource.Tables[tableName].DefaultView;
 comboBox.SelectedIndex = 0;
}

Then I do data binding on this combo box to my class. As my data binding is in another method different from above and combo box is created dynamically, I found my combo box by it's name in below method.

private void Method_1
{
control = this.Controls.Find(attrName, true); 
ComboBox specificControl = (ComboBox)control[0]; Binding attributeBinding = 
new Binding(SelectedValue, attributeSelectionController.FilterAttributeModels[attributeCount] , FilterValue, true, DataSourceUpdateMode.OnPropertyChanged, string.Empty, F); 
attributeBinding.ControlUpdateMode =   ControlUpdateMode.OnPropertyChanged;   specificControl.DataBindings.Add(attributeBinding);
 }

It seems fine till that state. But when binding event invoke and program flow go to the set property method, the value.GetType() return system.data.datarowview as type of selectedValue of combobox item instead of string or integer or any other datatype. I have tried to set value member for combox while I set datasource to its. But no help. Any one can help on it? I need urgent solution.

4

1 回答 1

1

终于可以找出问题所在了。在表单加载方法中,我更改了以下行的顺序。

comboBox.DisplayMember = attribute.Name; 
comboBox.DataSource = attributeValueDataSource.Tables[tableName].DefaultView;

作为

comboBox.DataSource = attributeValueDataSource.Tables[tableName].DefaultView;
comboBox.DisplayMember = attribute.Name;
combox.ValueMember = attribute.Name;

然后它工作。:)

**感谢帮助我的朋友。

于 2013-04-18T13:54:27.067 回答