原創|使用教程|編輯:龔雪|2017-08-30 16:50:03.000|閱讀 392 次
概述:
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
綁定到數據源的數據感知控件可以要求顯示附加項。這些項,由控件顯示但不存儲在其底層數據源中,被稱為虛擬或未綁定行。此示例演示如何使用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
結果如下圖所示:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn