我是WPF的新手。我创建了一个用于添加客户的窗口,并在VB中编写了代码隐藏,一切正常:条目已成功插入数据库。现在,我在刷新dataGrid时遇到了问题。
这是我的DataGridBinding
<Datagrid Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" IsReadOnly="True" x:Name ="DGCustomers" ItemsSource="{Binding Source={StaticResource PopulateCustomers}}" />
每当我打电话给
Me.TryCast(Me.FindResource("PopulateCustomers"), ObjectDataProvider).Refresh()
什么都没发生!。只有当我重新启动应用程序时,我才能看到更改!
这是我的XAML
<ObjectDataProvider x:Key="PopulateCustomers" ObjectType="{x:Type local:CustomersDataProvider}" MethodName="LoadDG" />
这是我的班级
Public Class CustomersDataProvider Dim con As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source= C:\Users\Nothing But WIND\Documents\DBSAMPLE.accdb" Dim oc As New OleDbConnection(con) Dim cmd As New OleDbCommand("SELECT * FROM Customers", oc) Dim da As New OleDbDataAdapter(cmd) Dim ds As New DataSet Dim dt As New DataTable Public Sub New() da.Fill(ds) dt = ds.Tables(0) End Sub Public Function LoadDG() Return dt.DefaultView End Function End Class
这是我的更新代码
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAdd.Click Dim cmd As New OleDbCommand("Select * FROM Customers", oc) Dim da As New OleDbDataAdapter(cmd) Dim ds As New DataSet Dim dt As New DataTable da.Fill(ds) dt = ds.Tables(0) dt.Rows.Add(txt_CustomerID.Text, txt_CustomerName.Text, txt_ContactPerson.Text, txt_Address.Text, txt_Area.Text, txt_City.Text, txt_Pincode.Text, txt_Number.Text, Chk_1Discount.IsChecked) If oc.State = ConnectionState.Closed Then oc.Open() End If Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(da) da.InsertCommand = cb.GetInsertCommand da.Update(dt) oc.Close() MsgBox("Item added") TryCast(Me.FindResource("PopulateCustomers"), ObjectDataProvider).Refresh() DGCustomers.Items.Refresh() End Sub
任何帮助都是非常感激的!