code_Finder
code_Finder

Reputation: 305

Combo box shows value member instead of Display Member

I have a combobox named cmbEmployeeNo and I populate it with a dataset in runtime.

private void populateEmployees()
{
    try
    {
        cmbEmployeeNo.DataSource = objNoPayBLL.getEmployees();
        cmbEmployeeNo.DisplayMember = "NAME_INI";
        cmbEmployeeNo.ValueMember="EMP_NO";
    }
    catch (Exception Ex)
    {
        MessageBox.Show(Ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

But instead of displaying "NAME_INI" it always shows "EMP_NO" when it is populated.

The objNoPayBLL.getEmployees() method provides data correctly.

I have the same combobox and the population method in another form and it works fine as expected. But this one does not.

Help me please.

Upvotes: 0

Views: 1418

Answers (4)

Harrichael
Harrichael

Reputation: 405

NAME_INI, your Display Member, needs to be implemented as a getter/setter

I encountered this same issue and the issue was the DisplayMember on my DataSource class did not have a get method and the ValueMember did. The combo box class expects these not just to be members, but get properties so when it doesn't see DisplayMember's get property, it fails silently and uses ValueMember as a default.

Upvotes: 1

Smruti Ranjan
Smruti Ranjan

Reputation: 1

DataGridView DisplayMember and Your DB coulmn Name both should match else it will throw Exception.Try To Use Some Alias name for UPPER(P.NAME_INI). I think that will work.

Upvotes: 0

code_Finder
code_Finder

Reputation: 305

I found the error accidently and its totally strange to me.

The oracle query for the data source of the cmbEmployeeNo combo box (Here I access it with objNoPayBLL.getEmployees() method) is as follows.

SELECT  A.EMP_NO,UPPER(P.NAME_INI) FROM  EMP_PROFILE P,EMP_ALLOCATION A WHERE A.EMP_NO=P.EMP_NO ORDER BY P.NAME_INI

I needed the employee name with initials in upper case and so I used UPPER(P.NAME_INI) here.But when I remove the UPPER(P.NAME_INI) and change it into just P.NAME_INI the combo box shows the display member properly.

SELECT  A.EMP_NO,P.NAME_INI FROM  EMP_PROFILE P,EMP_ALLOCATION A WHERE A.EMP_NO=P.EMP_NO ORDER BY P.NAME_INI

What the hell this oracle capital and simple do with C# combo box?

Upvotes: 0

user5930261
user5930261

Reputation:

There is a lot of validation happening in the background with this type of binding, try to set the datasource last and see if it helps

cmbEmployeeNo.DisplayMember = "NAME_INI";
cmbEmployeeNo.ValueMember="EMP_NO";
cmbEmployeeNo.DataSource = objNoPayBLL.getEmployees();

Upvotes: 0

Related Questions