我有一个DataGrid,它显示来自Access数据库的数据。基本上我要做的是使它在添加或更新数据库时,我想在Datagrid中显示它。
到目前为止我所拥有的计时器将每2秒更新一次:
Public Sub New()
_UserList = New List(Of Users)
showUsers()
Tmr.Interval = 2000
Tmr.Start()
End Sub
Private Sub TimerEventProcessor(myObject As Object, ByVal myEventArgs As EventArgs) Handles Tmr.Elapsed
showUsers()
OnPropertyChanged("SelectedCpuList")
OnPropertyChanged("UserList")
End Sub
这通常会起作用,但我也在检查那些涉及查看所选行并允许用户删除该记录的数据。这是它中断的地方,基本上每次单击一行时都会设置所选项,但showUsers()方法会再次将此值设置为空字符串。
Public ReadOnly Property UserList() As List(Of Users)
Get
Return _UserList
End Get
End Property
Public Sub showUsers()
Dim SelCpuID As String = ""
If hasRemoved = True Then
If _SelectedCpuList.Count <= 1 Then
_SelectedCpuList.Clear()
OnPropertyChanged("SelectedCpuList")
OnPropertyChanged("UserList")
hasRemoved = False
Else
OnPropertyChanged("SelectedCpuList")
OnPropertyChanged("UserList")
hasRemoved = False
End If
End If
For Each c In _SelectedCpuList
If String.IsNullOrEmpty(SelCpuID) OrElse SelCpuID.Length = 0 Then
SelCpuID = c.MachineID
End If
Next
_selUserList = SelCpuID
_MachineID = SelCpuID
_UserList = BLL.getUsers()
OnPropertyChanged("SelectedCpuID")
OnPropertyChanged("machineID")
End Sub
Private _SelectedCpuList As New List(Of Users)
Public Property SelectedCpuList() As List(Of Users)
Get
Return _SelectedCpuList
End Get
Set(ByVal value As List(Of Users))
_SelectedCpuList = value
OnPropertyChanged("SelectedCpuList")
showUsers()
End Set
End Property
Private _SelectedCpuID As Users
Public Property SelectedCpuID() As Users
Get
Return _SelectedCpuID
End Get
Set(ByVal value As Users)
_SelectedCpuID = value
OnPropertyChanged("SelectedCpuID")
Dim temp As New List(Of Users)
temp.Add(_SelectedCpuID)
SelectedCpuList = temp
End Set
End Property
这将遍历_SelectedCpuList(数据网格中的所选行)中的所有内容,如果所选字符串为空,则它将所选字符串设置为所选行的cpuID。
行SelCpuID = c.MachineID
给了我一个Null Reference Exception,我认为可能与更新DataGrid的不断尝试有关。
所以我认为这是一个错误的制作方法,当数据库中的内容被更改或添加时,必须有另一种方法来更新数据?另外,用户必须点击刷新按钮,如果他们忘记这样做可能会很痛苦。