LookupEdit in XtraGrid cell value goes blank
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:
An XtraGrid
with a column that has a combobox editor in each cell. The values of the dropdown options are different for different rows. Specifically the available options are subsets of ViewModel.Selectables
, the subset is defined by businessrules at runtime.
This is how I try to make this happen:
I create three BindingSources
viewModelBindingSource
: withDataSource = ViewModel
rowsBindingSource
: withDataSource = viewModelBindingSource
ANDDataMember = Rows
selectablesBindingSource
withDataSource = viewModelBindingSource
ANDDataMember = Selectables
I set the grid's DataSource
to rowsBindingSource
.
I create an In-place Editor Repository for a LookupEdit in the grid.
I set the repositoryItemLookUpEdit
's DataSource
to selecteablesBindingSource
I set the repositoryItemLookUpEdit
as the ColumnEdit
value of the column
I hook up to gridViews ShownEditor
event:
this.gridView1.ShownEditor += gridView1_ShownEditor;
In gridView1_ShownEditor(object sender, EventArgs e)
method I can then have a reference to the view so 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 options in the clicked combobox, and selecting the option sets the value to the bound object, that is Row.Selected
.
And now to my problem, when cell looses focus, the cell content turns blank.
This seems to be caused somehow by the fact that I create a new BindingSource
with new, because if I ommit this change of DataSource
then the values in ViewModel.Selectables
are used instead, and it works as expected.
So, does anyone know why the text displayed in the cell goes blank after i开发者_StackOverflow中文版t looses focus in this case??
I had the same issue few days back but i didn't find any solution for it. What i understood from it is that the values you are binidng to the grid column containing ComboEdit or LookupEdit must match the Vlaue Member value of the ComboEdit/LookUpEdit Collection.
If it gets find the matched value than it'll show the display member value in the cell otherwise the cell value will be blank.
This is what i got from my working experience on it.
I had a similar problem. In my case the problem was due to changing DataSource property of repositoryItemLookupEdit in the column.
When new DataSource in current row is more restricted and is not able to show other row's values, cells in those rows go blank.
To resolve this,
You may use the ShownEditor event and the code sample in the link below:
http://documentation.devexpress.com/#WindowsForms/DevExpressXtraGridViewsBaseColumnView_ShownEditortopic
The trick is,
Instead of setting the DataSource of repositoryItemLookupEdit, you get the view.ActiveEditor as a LookupEdit and set its DataSource. Then, other rows are not affected.
Here is a code sample:
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;
}
}
Ok so I figured part of it. I am not EXACTLY sure why the content goes blank. But it has to do with the fact that I had instantiated new objects that populate the list newSelectables
.
I guess that when the cell looses focus, the XtraGrid turns to the original repositoryItemLookUpEdit which points to ViewModel.Selectables to get the DisplayValue of the item. Since the selected item doesn't exist in the original list this fails. If I reuse the original objects instead of cloning them it seems to work.
You can override this behavior by adding an event handler on the Editor associated with the combobox. 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;
}
}
- Set DisplayMember of the LookUp to the name of the column that you wanna show
精彩评论