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
: with DataSource = ViewModel
rowsBindingSource
: with DataSource = viewModelBindingSource
AND DataMember = Rows
selectablesBindingSource
with DataSource = viewModelBindingSource
AND DataMember = 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 it 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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With