Back to Devexpress

RepositoryItemLookUpEdit.GetDataSourceRowByKeyValue(Object) Method

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemlookupedit-dot-getdatasourcerowbykeyvalue-x28-system-dot-object-x29.md

latest10.2 KB
Original Source

RepositoryItemLookUpEdit.GetDataSourceRowByKeyValue(Object) Method

Returns a data source row containing the specified RepositoryItemLookUpEditBase.ValueMember field value.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
public virtual object GetDataSourceRowByKeyValue(
    object keyValue
)
vb
Public Overridable Function GetDataSourceRowByKeyValue(
    keyValue As Object
) As Object

Parameters

NameTypeDescription
keyValueObject

The value of the RepositoryItemLookUpEditBase.ValueMember field for the row to be located.

|

Returns

TypeDescription
Object

An object representing the data source record containing the specified value. null ( Nothing in Visual Basic) if no record with the specified value in the RepositoryItemLookUpEditBase.ValueMember field was found.

|

Remarks

The lookup editor’s value is obtained from the data source field specified by the RepositoryItemLookUpEditBase.ValueMember property. The GetDataSourceRowByKeyValue method searches for the specified value within this field and returns an object representing the first found record.

The GetDataSourceRowByKeyValue method’s return value depends upon the type of the underlying data source. If the data source is a System.Data.DataTable or a System.Data.DataView, this method returns a System.Data.DataRowView object. If the data source is a custom list of items, the appropriate list item is returned.

Example

The following example shows how to customize a standalone Lookup editor to select a city:

In this example:

  • Persons and Cities lists contain Person and CityInfo objects. Person objects are linked to CityInfo objects.

  • The TextEdit and LookUpEdit controls edit the Person.Name and Person.CityID properties, respectively.

  • The lookup displays items from the Cities list.

  • The DataNavigator control navigates between items in the Persons list.

  • C#

  • VB.NET

csharp
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

public Form1() {
    InitializeComponent();
    List<Person> dsPersons = Person.Init();
    dataNavigator1.DataSource = dsPersons;
    textEdit1.DataBindings.Add(new Binding("EditValue", dsPersons, "Name"));
    lookUpEdit1.DataBindings.Add(new Binding("EditValue", dsPersons, "CityID"));
    lookUpEdit1.Properties.DataSource = CityInfo.Init();
    lookUpEdit1.Properties.ValueMember = "ID";
    lookUpEdit1.Properties.DisplayMember = "City";
}

public class Person {
    public string Name { get; set; }
    public int CityID { get; set; }
    static public List<Person> Init() {
        return new List<Person>() {
            new Person() { Name = "Carlos Gonzalez", CityID = 0 },
            new Person() { Name = "Mario Pontes", CityID = 5 },
            new Person() { Name = "Horst Kloss", CityID = 2 },
            new Person() { Name = "Martin Sommer", CityID = 3 },
            new Person() { Name = "Diego Roel", CityID = 3 },
            new Person() { Name = "Catherine Dewey", CityID = 4 }
        };
    }
}
public class CityInfo {
    int id;
    public CityInfo(int id) {
        this.id = id;
    }
    [Display(Order = -1)]
    public int ID { get { return id; } }
    public string City { get; set; }
    public string Country { get; set; }
    public string Region { get; set; }
    static public List<CityInfo> Init() {
        return new List<CityInfo>() {
            new CityInfo(0) { City = "Barquisimeto", Country = "Venezuela", Region = "Lara" },
            new CityInfo(1) { City = "Rio de Janeiro", Country = "Brazil", Region = "RJ" },
            new CityInfo(2) { City = "Cunewalde", Country = "Germany", Region = "" },
            new CityInfo(3) { City = "Madrid", Country = "Spain", Region = "" },
            new CityInfo(4) { City = "Charleroi", Country = "Belgium", Region = "" },
            new CityInfo(5) { City = "Sao Paulo", Country = "Brazil", Region = "SP" }
        };
    }
}
vb
Imports System
Imports System.Windows.Forms
Imports System.Collections.Generic
Imports System.ComponentModel.DataAnnotations

Public Sub New()
    InitializeComponent()
    Dim dsPersons As List(Of Person) = Person.Init()
    dataNavigator1.DataSource = dsPersons
    textEdit1.DataBindings.Add(New Binding("EditValue", dsPersons, "Name"))
    lookUpEdit1.DataBindings.Add(New Binding("EditValue", dsPersons, "CityID"))
    lookUpEdit1.Properties.DataSource = CityInfo.Init()
    lookUpEdit1.Properties.ValueMember = "ID"
    lookUpEdit1.Properties.DisplayMember = "City"
End Sub

Public Class Person
    Public Property Name() As String
    Public Property CityID() As Integer
    Public Shared Function Init() As List(Of Person)
        Return New List(Of Person)() From {
            New Person() With {.Name = "Carlos Gonzalez", .CityID = 0},
            New Person() With {.Name = "Mario Pontes", .CityID = 5},
            New Person() With {.Name = "Horst Kloss", .CityID = 2},
            New Person() With {.Name = "Martin Sommer", .CityID = 3},
            New Person() With {.Name = "Diego Roel", .CityID = 3},
            New Person() With {.Name = "Catherine Dewey", .CityID = 4}
        }
    End Function
End Class
Public Class CityInfo
    Private fid As Integer
    Public Sub New(ByVal id As Integer)
        fid = id
    End Sub
    <Display(Order := -1)>
    Public ReadOnly Property ID() As Integer
        Get
            Return fid
        End Get
    End Property
    Public Property City() As String
    Public Property Country() As String
    Public Property Region() As String
    Public Shared Function Init() As List(Of CityInfo)
        Return New List(Of CityInfo)() From {
            New CityInfo(0) With {.City = "Barquisimeto", .Country = "Venezuela", .Region = "Lara"},
            New CityInfo(1) With {.City = "Rio de Janeiro", .Country = "Brazil", .Region = "RJ"},
            New CityInfo(2) With {.City = "Cunewalde", .Country = "Germany", .Region = ""},
            New CityInfo(3) With {.City = "Madrid", .Country = "Spain", .Region = ""},
            New CityInfo(4) With {.City = "Charleroi", .Country = "Belgium", .Region = ""},
            New CityInfo(5) With {.City = "Sao Paulo", .Country = "Brazil", .Region = "SP"}
        }
    End Function
End Class

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the GetDataSourceRowByKeyValue(Object) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-lookup-display-custom-text-for-not-found-edit-value/CS/Form1.cs#L55

csharp
if(props != null && (e.Value is int)) {
    object row = props.GetDataSourceRowByKeyValue(e.Value);
    if(row == null) {

winforms-lookup-display-custom-text-for-not-found-edit-value/VB/Form1.vb#L55

vb
If props IsNot Nothing AndAlso (TypeOf e.Value Is Integer) Then
    Dim row As Object = props.GetDataSourceRowByKeyValue(e.Value)
    If row Is Nothing Then

See Also

DisplayMember

ValueMember

GetDisplayValueByKeyValue(Object)

GetDataSourceRowByDisplayValue(Object)

GetDataSourceRowByMultipleKeyValues(Object, Object[], String[])

GetKeyValueByDisplayValue(Object)

GetSelectedDataRow()

RepositoryItemLookUpEdit Class

RepositoryItemLookUpEdit Members

DevExpress.XtraEditors.Repository Namespace