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.