Error while filling a combobox

Do you have this problem:
Error: “Cannot bind to the new display member. Parameter name: newDisplayMember”, while filling the ValueMember

Then check if the name off the columns in the DataSource is the same as the value that you want to set.

System.Data.DataTable dt = fetchedTable;
            string strValueMember = string.Empty;
            string strDisplayMember = string.Empty;

            cbo.DataSource     = dt;
            cbo.ValueMember   = strValueMember;
            cbo.DisplayMember = strDisplayMember;
            cbo.SelectedValue  = -1;

fetchedTable is build like this
column name: ColumnOne, ColumnTwo
strValueMember = OUT_ColumnOne
strDisplayMember = OUT_ColumnTwo

On the moment you want to set the cbo.ValueMember then you get the error.
Update the strValueMember and strDisplayMember and the problem is gone.

But there is more !!
1. fill the ValueMebmer and the DisplayMember from the combobox.
2. Set the DataSource
now you THINK there is no error, but if you try to update the SelectedValue then again there is a problem
The solution is the same rename the values or the column names an all will go with out problem

Leave a Reply