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开发者_StackOverflowöß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:
BindingSource bindingSource = new BindingSource(PS.PaperSizes, null);
lueSizes.Properties.DataSource = bindingSource;
lueSizes.Properties.Columns.Add(new LookUpColumnInfo("PaperName", "Größe"));
lueSizes.Properties.DisplayMember = "PaperName";
foreach (PaperSize size in bindingSource)
if (size.RawKind == BinSettings.SizeRawKind)
{
lueSizes.EditValue = size;
break;
}
private void lueSizes_EditValueChanged(object sender, EventArgs e)
{
PaperSize size = lueSizes.EditValue as PaperSize;
Update(size);
}
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.
I know this is an old thread but I just got the same problem. The accepted answer is not the best practice since our goal is to get the selected object rather than the key value. The workaround is to call lookupEdit.Properties.ForceInitialize() right after EditValueChangedEvent got fired and GetSelectedDataRow() should work afterward. This solve the problem if the lookupEdit is not changed via mouse.
You have to insure that the assigned value is exists in the lookupEdit's datasource,
then try set the .Text
property directly
To pull the underlying assigned object
PaperSize selectedPS = (PaperSize)lueSizes.Properties.GetDataSourceRowByDisplayValue(lueSizes.Text)
精彩评论