I am aware that there other questions like this, but their solutions do not work for me.
I have the following code:
lueSizes.Properties.DataSource = new BindingSource(PS.PaperSizes, null); lueSizes.Properties.Columns.Add(new LookUpColumnInfo("PaperName", "Größe")); lueSizes.Properties.DisplayMember = "PaperName"; lueSizes.Properties.ValueMember = "PaperName"; //PS is a regular System PrinterSettings object foreach (PaperSize size in PS.PaperSizes) //I confirmed with debugging that this actually happens correctly if (size.RawKind == binSettings.SizeRawKind) { lueSizes.EditValue = size; break; }
Populating the LookupEdit with the DataSource works fine, the user can select the desired PaperSize from the dropdown, and
lueSizes.GetSelectedDataRow() as PaperSize
then returns a PaperSize object as expected.
The problem I have is setting the EditValue, it simply does nothing. I have verified that at runtime, the DataSource contains all the PaperSize objects in PS.PaperSizes, including the one that is found in the foreach loop. But setting EditValue = size does not cause the selected data row to update accordingly.
Other variations I have tried are:
lueSizes.EditValue = size.PaperName;
lueSizes.EditValue = lueSizes.Properties.GetKeyValueByDisplayText(size.PaperName);
lueSizes.EditValue = lueSizes.Properties.GetKeyValueByDisplayValue(size.PaperName);
lueSizes.EditValue = lueSizes.Properties.GetKeyValueByDisplayValue(size);
lueSizes.EditValue = 0;
None of these do anything, the selected datarow remains NULL and displays nothing to the user.
What else can I try to set the selected DataRow by code?
Edit:
private void lueSizes_EditValueChanged(object sender, EventArgs e)
{
object o = lueSizes.EditValue;
object p = lueSizes.GetSelectedDataRow();
PaperSize size = o as PaperSize;
UpdateSize(size);
}
Object o is the item I have set earlier, the PaperSize size that Ive found in the loop, but object p is null.
I think I have at least found a workaround:
So first, I let the loop search in the BindingSource, which I have to define explicitely now, instead of the Printersettings object.
Next, I may not set the DisplayValue property.
Finally, I avoid looking up the DataRow and go for the edit value directly. Don’t know what limitations
I don’t know what else that breaks, if anything, but for now it works.