我从文本框继承了自定义控件我添加了一个自定义属性,如下所示
Dim _MyVal As Object
Public Property MyVal As Object
Get
Return _MyVal
End Get
Set(value As Object)
_MyVal = value
If IsNothing(value) OrElse IsDBNull(value) Then
Me.Text = "Null"
Else
Select Case _MyVal
Case 1
Me.Text = "NewYork"
Case 2
Me.Text = "London"
Case 3
Me.Text = "Zwara"
Case Else
Me.Text = "Unknown"
End Select
End If
End Set
End Property
所以我用 DataGridView 和自定义文本框将这个属性绑定到简单 WinForm 中的 DataTable 字段,如下所示
Public Class Form10
Dim dtMain As New DataTable
Dim bsMain As New BindingSource
Private Sub Form10_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
dtMain.Columns.Add("Name", GetType(String))
dtMain.Columns.Add("CityID", GetType(Integer))
dtMain.Rows.Add("John", 1)
dtMain.Rows.Add("Steve", 2)
dtMain.Rows.Add("Sara", 3)
dtMain.Rows.Add("Joe", DBNull.Value)
bsMain.DataSource = dtMain
dgv.DataSource = bsMain
txtCity.DataBindings.Add(New System.Windows.Forms.Binding("MyVal", bsMain, "CityID", False, DataSourceUpdateMode.OnPropertyChanged))
dtMain.AcceptChanges()
End Sub
我的问题是,当我浏览项目时,DataTable 发生了变化。为什么会这样?
当我将该字段绑定到 ComboBoxSelectedValue
属性时,它可以完美运行。它不会通过导航改变来源;当我更改SelectedValue
属性时它会改变