KB Article T191121
Visible to All Users

Grid - How to use LookUp editors with different sources within a single column via the CustomRowCellEdit event

The main idea of this approach is to use different repository items with their own sources for different cells instead of a column’s repository item. To achieve that objective, we provide the CustomRowCellEdit event, which fires each time an editor has been invoked. The event handler receives an argument of the CustomRowCellEditEventArgs type, which has the RepositoryItem property. Assigning a new repository item to this property will help you achieve the required result. For more information, you can refer to the Assigning Editors to Individual Cells help topic.
There are two ways of customizing the CustomRowCellEdit event handler in the most productive manner. In my examples, I will use RepositoryItemGridLookUpEdit.
- If there are not so many variants of the editor data source, the best idea will be to create a predefined set of RepositoryItem instances with required sources. For example:

C#
RepositoryItemGridLookUpEdit firstRep = new RepositoryItemGridLookUpEdit(); RepositoryItemGridLookUpEdit secondRep = new RepositoryItemGridLookUpEdit(); firstRep.DataSource = GetDataForFirstRep(); secondRep.DataSource = GetDataForSecondRep(); … void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) { if (e.Column == gridView1.Columns[0]) { if (gridView1.GetDataSourceRowIndex(e.RowHandle) % 2 == 0){ // here can be any condition(s) based on your project’s logic e.RepositoryItem = firstRep; } else { e.RepositoryItem = secondRep; } } }
Visual Basic
Private firstRep As New RepositoryItemGridLookUpEdit() Private secondRep As New RepositoryItemGridLookUpEdit() firstRep.DataSource = GetDataForFirstRep() secondRep.DataSource = GetDataForSecondRep() '… Private Sub gridView1_CustomRowCellEdit(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) If e.Column = gridView1.Columns(0) Then If gridView1.GetDataSourceRowIndex(e.RowHandle) Mod 2 = 0 Then ' here can be any condition(s) based on your project’s logic e.RepositoryItem = firstRep Else e.RepositoryItem = secondRep End If End If End Sub

- If the data source number is dynamic, RepositoryItems can be created directly from the CustomRowCellEdit event handler and stored in the Dictionary object. Use a value based on which you assign a data source to LookUp as a key in the Dictionary:

C#
Dictionary<object, RepositoryItemGridLookUpEdit> dictionary = new Dictionary<object, RepositoryItemGridLookUpEdit>(); … void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e) { if (e.Column == gridView1.Columns[0]) { object key = gridView1.GetRowCellValue(e.RowHandle, gridView1.Columns[1]); e.RepositoryItem = GetDynamicRepository(key); } } private RepositoryItemGridLookUpEdit GetDynamicRepository(object key) { if (!(dictionary.ContainsKey(key))) { RepositoryItemGridLookUpEdit rep = new RepositoryItemGridLookUpEdit(); gridControl1.RepositoryItems.Add(rep); rep.DataSource = GetSomeDataSource(key); // This method returns a data source based on the key object. You need to implement it manually dictionary.Add(key, rep); } return dictionary[key]; }
Visual Basic
Private dictionary As New Dictionary(Of Object, RepositoryItemGridLookUpEdit)() '… Private Sub gridView1_CustomRowCellEdit(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs) If e.Column = gridView1.Columns(0) Then Dim key As Object = gridView1.GetRowCellValue(e.RowHandle, gridView1.Columns(1)) e.RepositoryItem = GetDynamicRepository(key) End If End Sub Private Function GetDynamicRepository(ByVal key As Object) As RepositoryItemGridLookUpEdit If Not(dictionary.ContainsKey(key)) Then Dim rep As New RepositoryItemGridLookUpEdit() GridControl1.RepositoryItems.Add(rep) rep.DataSource = GetSomeDataSource(key) ' This method returns a data source based on the key object. You need to implement it manually dictionary.Add(key, rep) End If Return dictionary(key) End Function

Note, this approach can be used not only for RepositoryItemGridLookUpEdit, but for RepositoryItemSearchLookUpEdit and RepositoryItemTreeListLookUpEdit as well.
See also:
How to filter a second LookUp column based on a first LookUp column's value
How to optimize the XtraGrid's performance
How to automatically open the popup window of a dropdown in-place editor

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.