65.9K
CodeProject 正在变化。 阅读更多。
Home

BindingList 示例

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.44/5 (15投票s)

2008年1月26日

CPOL

3分钟阅读

viewsIcon

180416

downloadIcon

4304

一篇关于如何创建业务对象的文章。

引言

这是一个非常基础的示例,展示了如何使用 System.ComponentModel.BindingList。 这是系列文章中的第一篇,将引导新手开发人员从简单的数据绑定到创建父/子基础业务对象。

背景

我写这些文章的原因是希望帮助其他开发人员理解如何创建业务对象。 如果你曾经看过一些可以下载和使用的免费框架,例如 CSLA (http://www.lhotka.net/cslanet/),我没有聪明到可以把它拉下来并说,是的,我理解它是如何工作的! 因此,虽然我的意图不是重新创建任何特定的框架,甚至不是创建一个新的框架,但我确实希望展示一些可以用来创建强大的父/子业务对象的基本功能。

使用代码

此示例在 VB.NET 和 C# 中均可用于 VS 2005。 我不想在这里发布所有代码,所以请下载 Zip 文件以获取完整示例。 这是一个简单的表单,其中包含一个名为 dgvEmployeesDataGridView 和一个调用 Populate 方法的 Button

' This import is needed so we do not need to user
' the Fully Qualified Name (FQN) for BindingList
Imports System.ComponentModel

Public Class frmGenericBinding

#Region " Modular Variables "
    ' Provides generic collection data binding. Notice that we are specifying 
    ' the type of object that is allowed to be added to the BindingList
    Private moEmployeeBindingList As BindingList(Of Employee)
#End Region

#Region " Constructors "
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Instantiate the needed BindingList
        moEmployeeBindingList = New BindingList(Of Employee)

    End Sub
#End Region

#Region " Private Methods "

''' <summary>

''' This method will create 50 new employees and add them to the BindList

''' </summary>

Private Sub PopulateBindingList()
    Dim oEmployee As Employee

    ' Create some Employees
    For nCnt As Integer = 1 To 50
        oEmployee = New Employee

        ' Create a employee first/last name using the counter
        oEmployee.FirstName = "Employee_" & nCnt.ToString
        oEmployee.LastName = "Last_" & nCnt.ToString
        oEmployee.MiddleInitial = "A"

        ' Create a employee street address using the counter
        oEmployee.Address1 = "12" & nCnt.ToString() & " Happy Street"
        oEmployee.City = "Charlotte"
        oEmployee.State = "NC"
        oEmployee.ZipCode = "28211"

        ' Create a manager id - Generate some ID
        oEmployee.Manager = Guid.NewGuid

        ' Add new employee to the BindingList
        moEmployeeBindingList.Add(oEmployee)
        
        // This will compile but will throw a System.InvalidCastException
        // when executed
        //moEmployeeBindingList.Add(new object)

        Next
End Sub

#End Region

Private Sub btnLoadEmployees_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnLoadEmployees.Click
    PopulateBindingList()

    ' Bind the BindingList of Employee objects to the DataGridView
    Me.dgvEmployees.DataSource = moEmployeeBindingList

End Sub

End Class

看看上面的代码,我们将从 BindingList 对象开始。 行"Private moEmployeeBindingList As BindingList(Of Employee)" 声明一个 BindingList 对象并说明它只会保存 Employee 类型的对象。 如果你以前没有这样做过,可能看起来没什么大不了的。 但是,如果你允许任何类型的对象进入集合,然后尝试绑定该集合或遍历该集合会发生什么? 如果你遍历集合以查找 FirstName 字段,并且集合中的某个项目没有该字段,则会抛出错误。 所以,类型安全是一件好事!

接下来要注意的是 "btnLoadEmployees_Click"。 此方法为我们创建 50 个新的 employee 对象并将它们放入 BindingList,并将 DataGridDataSource 设置为 BindingList。 要显示我们的 Employee 对象集合,我们所要做的就是创建 Employee 对象,将它们放入 BindingList(它现在用作我们的集合对象以及处理绑定),并设置 DataSource

我们的数据在 DataGrid 中,现在怎么办? 好吧,选择网格中的任何单元格并双击它,以便你可以编辑单元格的值。 还要确保你正在查看“输出”窗口。 现在,更改任何值并跳出该单元格或单击另一个单元格。 请注意,在“输出”窗口中,你将看到一条消息,指出“属性 xx 已更改”。 当你更改值时,BindingList 会将该更改持久保存回其 Employee 对象。 如果你问我,这太酷了。

这是第一部分的结尾。 在下一部分中,我们将创建自己的从 BindingList 继承的集合对象,并向我们的 Employee 对象添加更多逻辑。

© . All rights reserved.