用先前的数据对DataGridView排序后重新填充数据适配器的问题

时间:2018-09-05 19:27:11

标签: vb.net sorting datagridview

我有一个未绑定的datagridview,它用一个按钮填充了数据适配器和datagrid。它可以正常工作并填充数据网格。但是,如果我对列进行排序并尝试重新填充数据网格,它将引发异常Object reference not set to an instance of an object.

If Not IsNothing(dgVendors.DataSource) Then
    Call CType(dgVendors.DataSource, DataTable).Rows.Clear()
    Call CType(dgVendors.DataSource, DataTable).Columns.Clear()
    dgVendors.DataSource = Nothing
End If
dgVendors.Refresh()

Try
    Dim sdt As Date = DateAdd(DateInterval.Year, -2, Now())
    If HQCon.State = ConnectionState.Closed Then HQCon.Open()
    Dim strSQL As String = "Select * FROM Vendors"
    Dim da1 As SqlClient.SqlDataAdapter = New SqlClient.SqlDataAdapter(strSQL, HQCon)

    da1.Fill(ds, "vnd")
    dgVendors.DataSource = ds.Tables("vnd")
    da1.Fill(ds)
    Dim dt As DataTable = ds.Tables("vnd")
    If HQCon.State = ConnectionState.Open Then HQCon.Close()
    da1.Dispose()
    dt.Dispose()
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Critical Or MsgBoxStyle.OkOnly, "Error")
Finally
    If HQCon.State = ConnectionState.Open Then HQCon.Close()
End Try

0 个答案:

没有答案