How to display controls based on Combobox Selection in Windows Application at edit time?

Viewed 534

Value type 1

Value Type 2

I am using Windows application and created one form for Add and Edit mode. The issue is on Add it works fine, but on edit, controls are not displaying based on combobox selection. As per my combobox selection change event, I have hidden the controls. But my combobox is not selecting values and not triggering change event also. Code is :

//Edit Mode        

public CompanyAddEdit(MainForm form, string id)
{
    InitializeComponent();
    passedForm = form;
    var cmbList = BindCompanyType();
    isEdit = true;
    xmlDocPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Companies.xml");
    xDocument = XDocument.Load(xmlDocPath);
    Id = Convert.ToInt32(id);
    XElement company = xDocument.Descendants("Company").FirstOrDefault(p => p.Element("Id").Value == Id.ToString());

    if (company != null)
    {
        var type = company.Element("DataSourceType").Value;
        cmbbx_companyType.SelectedItem = type;
    }
}

I am binding Combobox using this method :

private Array BindCompanyType()
{
    var companyTypeList = Enum.GetValues(typeof(CompanyType));
    cmbbx_companyType.DataSource = companyTypeList;           
    return companyTypeList;
}

How can I fixed this? Any help will be appreciated.

1 Answers

So you have populated the combobox with items of type CompanyType enum. so the selected item should also the same type. Hope that you are getting a string from company.Element("DataSourceType").Value; so you can modify the code like the following:

cmbbx_companyType.SelectedItem = Enum.Parse(typeof(CompanyType),type);        

Please make a try and let me know whether it solve the issues or not.

Related