LookupEdit in XtraGrid cell value goes out

This is what I have:

public class ViewModel { public BindingList<Row> Rows { get; set; } public BindingList<MyElement> Selectables { get; set; } } public class Row { public MyElement Selected { get; set; } } public class MyElement { public string Value { get; set; } public string Friendly { get; set; } } 

This is what I want: XtraGrid with a column that has a combobox editor in each cell. The values ​​of the dropdown parameters are different for different lines. In particular, the available options are subsets of ViewModel.Selectables ; the subset is determined by business servers at runtime.

Here is how I am trying to do this:

I create three BindingSources

  • viewModelBindingSource : with DataSource = ViewModel

  • rowsBindingSource : with DataSource = viewModelBindingSource AND DataMember = Rows

  • selectablesBindingSource with DataSource = viewModelBindingSource AND DataMember = Selectables

I set the DataSource grid to rowsBindingSource . I am creating an in-place editor repository for LookupEdit in the grid. I set repositoryItemLookUpEdit DataSource to selecteablesBindingSource I set repositoryItemLookUpEdit as the ColumnEdit value of the column

I connect to gridViews ShownEditor event:

 this.gridView1.ShownEditor += gridView1_ShownEditor; 

In the gridView1_ShownEditor(object sender, EventArgs e) method, I can then reference the view so that I can do something like this:

 GridView view = sender as GridView; var newSelectables = new BindingList<MyElement>(); // businesslogic to populate newSelectables ... var bs = new BindingSource(newSelectables, ""); edit = (LookUpEdit)view.ActiveEditor; edit.Properties.DataSource = bs; 

This works to the extent that I get the new parameters in brackets with a click, and selecting an option sets a value for the associated object, i.e. Row.Selected .

And now to my problem, when a cell loses focus, the contents of the cell become empty .

This is apparently due to the fact that I am creating a new BindingSource with a new one, because if I omit this DataSource change, the values ​​in ViewModel.Selectables are used instead and it works as expected.

So, does anyone know why the text displayed in the cell disappears after it loses focus in this case?

+4
source share
5 answers

I had the same problem a few days ago, but I did not find any solution for it. What I understood from this is that the values ​​you load into the grid column containing ComboEdit or LookupEdit must match the Vlaue Member value from the ComboEdit / LookUpEdit collection.

If it finds a matching value, than it displays the value of the display element in the cell, otherwise the cell value will be empty.

This is what I got from my experience working on it.

+3
source

I had a similar problem. In my case, the problem was related to changing the DataSource property of the repositoryItemLookupEdit object in the column.
When a new DataSource in the current row is more limited and cannot display other row values, the cells in these rows remain empty.

To solve this problem, you can use the ShownEditor event and sample code in the link below:
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsBaseColumnView_ShownEditortopic

The trick is, Instead of setting the DataSource for repositoryItemLookupEdit, you get view.ActiveEditor as LookupEdit and set its DataSource. Then the other lines are not affected.
Here is a sample code:

 void eAdvBandedGridView1_ShownEditor(object sender, EventArgs e) { GridView view = sender as GridView; if (view.FocusedColumn.FieldName == "CenterID" && view.ActiveEditor is LookUpEdit) { LookUpEdit editor = view.ActiveEditor as LookUpEdit; vVoucherItemInfoDTO item = view.GetFocusedRow() as vVoucherItemInfoDTO; if (lastFetchedAccount == null || lastFetchedAccount.ID != item.AccountID) { lastFetchedAccount = accountServiceClient.GetAccountInfo(item.AccountID); } if (lastFetchedAccount.AllowAllCenters) editor.Properties.DataSource = GlobalDataStore.CenterList; else editor.Properties.DataSource = lastFetchedAccount.AllowedCenterList; } } 
+3
source

Ok, so I decided some of this. I am NOT JUST sure why the content is empty. But this is due to the fact that I created new objects that populate the newSelectables list. I assume that when the cell loses focus, XtraGrid accesses the original ItemLookUpEdit repository, which points to ViewModel.Selectables to get the DisplayValue of the element. Since the selected item does not exist in the source list, this is not performed. If I reuse the original objects instead of cloning them, they seem to work.

+2
source

You can override this behavior by adding an event handler to the Editor associated with the drop-down list. eg,

  private void goalTypeEditor_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e) { if (e.DisplayText == "") { string goalTypeId = (string)e.Value; RefDataSet refData = ((IParentView)ParentView).RefData; string goalTypeLabel = refData.GoalType.FindByGoalTypeID(goalTypeId).Label; e.DisplayText = "(No longer in use) " + goalTypeLabel; } } 
+1
source
  • Set DisplayMember LookUp to the name of the column you want to show
0
source

All Articles