原創(chuàng)|使用教程|編輯:龔雪|2017-08-30 16:50:03.000|閱讀 392 次
概述:
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
綁定到數(shù)據(jù)源的數(shù)據(jù)感知控件可以要求顯示附加項(xiàng)。這些項(xiàng),由控件顯示但不存儲在其底層數(shù)據(jù)源中,被稱為虛擬或未綁定行。此示例演示如何使用UnboundSource組件實(shí)現(xiàn)這些行。
1. 使用lookup editor時(shí)經(jīng)常需要虛擬行,這些行通常需要在其下拉列表中顯示“全部”或“無”等服務(wù)項(xiàng)。因此,首先將UnboundSource組件和LookUpEdit控件添加到表單中。
2. 使用UnboundSource組件為lookup editor提供數(shù)據(jù)。本文將介紹整個(gè)過程。以下是您需要做什么的簡要說明。
· 準(zhǔn)備外部數(shù)據(jù)集。對于此示例應(yīng)用程序,您可以使用填充有簡單實(shí)體的列表結(jié)構(gòu),如下所示:
[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
· 將數(shù)據(jù)字段添加到您的UnboundSource組件。字段名稱必須參考現(xiàn)有的數(shù)據(jù)集字段。
· 將UnboundSource組件分配給編輯器的RepositoryItemLookUpEditBase.DataSource屬性。
· 指定編輯器的RepositoryItemLookUpEditBase.DisplayMember和RepositoryItemLookUpEditBase.ValueMember屬性。
3. 調(diào)用組件的SetRowCount方法來指定“查找”應(yīng)顯示的項(xiàng)數(shù)。通常,您將使用實(shí)際數(shù)據(jù)集行計(jì)數(shù)作為方法的參數(shù)。由于我們需要多個(gè)附加行,請使用如下所示的較高值。
[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
結(jié)果如下圖所示:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn