绑定到数据源的数据感知控件可以要求显示附加项。这些项,由控件显示但不存储在其底层数据源中,被称为虚拟或未绑定行。此示例演示如何使用UnboundSource组件实现这些行。
1. 使用lookup editor时经常需要虚拟行,这些行通常需要在其下拉列表中显示“全部”或“无”等服务项。因此,首先将UnboundSource组件和LookUpEdit控件添加到表单中。
2. 使用UnboundSource组件为lookup editor提供数据。本文将介绍整个过程。以下是您需要做什么的简要说明。
· 准备外部数据集。对于此示例应用程序,您可以使用填充有简单实体的列表结构,如下所示:
[C#]
public partial class Form1 : Form { public List<Product> Products = new List<Product>(); public Form1() { InitializeComponent(); Products.AddRange(new Product[] { new Product(1, "Item One"), new Product (2, "Item Two"), new Product(3, "Item Three")}); } } public class Product { public Product(int ID, string productName) { this.ID = ID; this.ProductName = productName; } public int ID { get; set; } public string ProductName { get; set; } }
[VB]
Partial Public Class Form1 Inherits Form Public Products As New List(Of Product)() Public Sub New() InitializeComponent() Products.AddRange(New Product() { New Product(1, "Item One"), New Product(2, "Item Two"), New Product(3, "Item Three") }) End Sub End Class Public Class Product Public Sub New(ByVal ID As Integer, ByVal productName As String) Me.ID = ID Me.ProductName = productName End Sub Public Property ID() As Integer Public Property ProductName() As String End Class
· 将数据字段添加到您的UnboundSource组件。字段名称必须参考现有的数据集字段。
· 将UnboundSource组件分配给编辑器的RepositoryItemLookUpEditBase.DataSource属性。
· 指定编辑器的RepositoryItemLookUpEditBase.DisplayMember和RepositoryItemLookUpEditBase.ValueMember属性。
3. 调用组件的SetRowCount方法来指定“查找”应显示的项数。通常,您将使用实际数据集行计数作为方法的参数。由于我们需要多个附加行,请使用如下所示的较高值。
[C#]
public const int NotInListItemsCount = 2; unboundSource1.SetRowCount(Products.Count + NotInListItemsCount);
[VB]
Public Const NotInListItemsCount As Integer = 2 unboundSource1.SetRowCount(Products.Count + NotInListItemsCount)
4. 最后,处理组件的ValueNeeded事件并添加所需的虚拟行。
[C#]
void unboundSource1_ValueNeeded(object sender, DevExpress.Data.UnboundSourceValueNeededEventArgs e) { e.Value = GetExtendedListValues(e.RowIndex, e.PropertyName); } object GetExtendedListValues(int rowIndex, string propertyName) { switch(propertyName) { case "ID": switch(rowIndex) { case 0: return null; case 1: return null; default: return Products[rowIndex - NotInListItemsCount].ID; } case "ProductName": switch(rowIndex) { case 0: return "None"; case 1: return "All"; default: return Products[rowIndex - NotInListItemsCount].ProductName; } default: return null; } }
[VB]
Private Sub unboundSource1_ValueNeeded(ByVal sender As Object, ByVal e As DevExpress.Data.UnboundSourceValueNeededEventArgs) e.Value = GetExtendedListValues(e.RowIndex, e.PropertyName) End Sub Private Function GetExtendedListValues(ByVal rowIndex As Integer, ByVal propertyName As String) As Object Select Case propertyName Case "ID" Select Case rowIndex Case 0 Return Nothing Case 1 Return Nothing Case Else Return Products(rowIndex - NotInListItemsCount).ID End Select Case "ProductName" Select Case rowIndex Case 0 Return "None" Case 1 Return "All" Case Else Return Products(rowIndex - NotInListItemsCount).ProductName End Select Case Else Return Nothing End Select End Function
结果如下图所示:
慧都学院2017全新DevExpress线下研修班火热报名中!
- WinForms控件学习+实践的不二之选——DevExpress WinForm提升班
- 高效的MVVM开发模式,WPF开发首选培训平台——DevExpress WPF提升班
- 手把手将您快速开发出完美、强大的ASP应用程序——DevExpress ASP.NET提升班
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:DevExpress控件中文网 [https://www.devexpresscn.com/]
本文地址:https://www.devexpresscn.com/post/910.html