none
VB.NET 链接数据出现问题(WPF DataGrid) RRS feed

  • 问题

  • 大家好,我这里有一段代码:

    xaml:

            <DataGrid x:Name="DataViewMain" HorizontalAlignment="Left" Height="515" Margin="10,45,10,10" VerticalAlignment="Top" Width="974">
                <DataGrid.Background>
                    <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.AppWorkspaceColorKey}}"/>
                </DataGrid.Background>
            </DataGrid>

    vb.net:

    
    
        Sub ConnectToData()
            Dim objconn As New OleDb.OleDbConnection
            Dim objcomm As New OleDb.OleDbCommand
            '连接字符串
            objconn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\database\db.mdb"
            '告诉程序选择那些列
            objcomm.CommandText = "Select ID , name From table1"
            'objconn和objcomm之间的数据连接
            objcomm.Connection = objconn
            objda.SelectCommand = objcomm
            Dim builder As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(objda)
            objconn.Open()
            objda.Fill(objds, "table1")
            objdt = objds.Tables("table1")
            objconn.Close()
            DataViewMain.ItemsSource = objdt.DefaultView
        End Sub
        '刷新数据
        Sub RefreshData()
            objda.Update(objds, "table1")
            objdt.Clear()
            ConnectToData()
        End Sub

    当按下“刷新”按钮时(前面的xaml代码没有提到),运行这段代码:

            objda.Fill(objds, "table1")
            objdt.Clear()
            RefreshData()

    结果在"objdt.Clear()" 发生了一个错误:System.NullReferenceException:"未将对象引用设置到对象的实例。" 引发这种错误的原因很多,我改了很多地方都不见效果,请问要怎么办呢?

    谢谢

    2018年11月2日 10:05

答案

全部回复