窗体的控件不更新数据集更改

时间:2019-04-30 12:08:34

标签: sql vb.net

我可以看到DataGridView发生了很好的变化,但是窗体的控件没有更新。
据我所知bs.ResetBindings(False)应该触发更新!
我已经尝试从属性绑定选项卡绑定控件,并且结果相同!

但是我必须承认这是我在vb.net上的首次尝试!

Imports System.Data.SqlClient

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        testdatTableAdapter.Fill(DATAHUBDataSet.testdat)
        bs = New BindingSource

        address.DataBindings.Add("text", bs, "address")
        typep.DataBindings.Add("text", bs, "type_projet")
        env.DataBindings.Add("text", bs, "enveloppe")
        ra.DataBindings.Add("text", bs, "region")
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'If Not IsNothing(ds.Tables("testdat")) Then
        '    ds.Tables.Clear()
        'End If

        da = Nothing
        ds = Nothing
        qry = "Select * from work.testdat where network = "
        qry &= "'" & resstrb.Text & "'"
        con.Close()
        con.Open()
        'qry = "Select * from work.testdat"
        cmd = New SqlCommand(qry, con)
        da = New SqlDataAdapter(cmd)
        ds = New DataSet
        da.Fill(ds, "testdat")
        DataGridView1.DataSource = ds.Tables("testdat")
        bs.ResetBindings(False)
    End Sub
End class

Imports System.Data.SqlClient
'! note to mod, can't get this last bit of code to work with ctrl K 
Module test_sqldata
    Public str As String = getconstring()
    Public con As New SqlConnection(str)
    Public cmd As SqlCommand
    Public da As SqlDataAdapter
    Public ds As DataSet
    Public bs As BindingSource
    Public qry As String

    Public Function getconstring() As String
        Return "Data Source=XXXXXXX\YYYYYYYY;Initial Catalog=DATAHUB;Integrated Security=True"
    End Function
End Module

1 个答案:

答案 0 :(得分:1)

将“ bs”绑定到ds.datatable或删除“ bs”并将文本框绑定到“ ds.Tables(“ testdat”)'。如果您可以清楚地看到网格和文本框正在使用2不同的数据源,根本没有设置bindingsource数据源,因此文本框被绑定到一个空的bindingsource