4

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

  1. viewModelBindingSource: with DataSource = ViewModel

  2. rowsBindingSource: with DataSource = viewModelBindingSource AND DataMember = Rows

  3. 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??

amiry jd
  • 27,021
  • 30
  • 116
  • 215
Manolo
  • 1,597
  • 4
  • 21
  • 35
  • Have you bound anything to the grids datasource? You must have a datasource when adding data to a grid otherwise it blanks. You can pre-initialise it in the InitNewRow event. – Paul Talbot Sep 26 '11 at 14:36
  • @Sres, yes as I describe I set the grids dataSource to the BindingSource rowsBindingSource, which is bound to Rows in the ViewModel class. The grid is not blank. – Manolo Sep 26 '11 at 16:52

5 Answers5

3

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;

    }
}
Veysel Ozdemir
  • 675
  • 7
  • 12
  • This worked well for me when I wanted each row in the grid to have a potentially different list of allowable items in the dropdown. – Coxy Jun 09 '14 at 07:03
3

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.

Syeda
  • 1,215
  • 11
  • 23
  • Right, if you by "the values you are binding to the grid column", mean the collection I am binding to the lookupEditRepositoryItem that is attached to that column i.e. Selectables in the example, and by "the value member value of the LookUpEdit collection" you are referring to the collection that I set to the lookupEdit in the event handler method, i.e. newSelectables, then I agree. – Manolo Sep 27 '11 at 07:17
  • You have to also make sure that there is only one item in the bound collection that has the id that you have selected, otherwise the entry will appear as blank. – Maciej Jan 10 '13 at 15:02
2

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.

Manolo
  • 1,597
  • 4
  • 21
  • 35
1

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;
        }
    }
Chui Tey
  • 5,436
  • 2
  • 35
  • 44
0
  • Set DisplayMember of the LookUp to the name of the column that you wanna show
LTN
  • 1