2

我在更新 VS 2012 中的 VB.net Windows 窗体中的表时遇到了一些问题

我已经从服务数据库设置了数据库,它只需要在本地存储少量数据

当我创建一个新用户时我想要做什么它会更新数据网格视图

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    BDate1.Format = DateTimePickerFormat.Custom
    BDate1.CustomFormat = "MM/dd/yyyy"

    If fname.Text <> "" And lname.Text <> "" Then
        If Not cn.State = ConnectionState.Open Then
            cn.Open()
        End If
        ' cn1.Open()
        If rb1.Checked Then
            gen = rb1.Text.ToString
        ElseIf rb2.Checked Then
            gen = rb2.Text.ToString
        End If

        cmd.CommandText = "INSERT INTO StudentTB (FirstName,LastName,Birthday,Avatar,Gender,Grade) values('" & fname.Text & "', '" & lname.Text & "', '" & BDate1.Text & "', '" & AvtarNM.Text & "', '" & gen & "', '" & TxtGrade.Text & "')"
        Dim dt As New DataTable
        dt.Load(cmd.ExecuteReader())
        DataGridView1.DataSource = dt
        ' cmd.ExecuteNonQuery()
        cn.Close()
        fname.Text = ""
        lname.Text = ""

问题是我可以清除表格并使用按钮重新加载它,但它不会显示更新的值而不关闭应用程序然后重新打开它,我尝试了一些事情,似乎没有任何东西可以刷新连接和/或更新数据库。任何帮助都会有所帮助。

谢谢

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    'DataGridView1.DataSource = Nothing
     Database1DataSet1.StudentTB.Clear()
    ' Database1DataSet1.refresh()
    ' Dim myda As String
    '  cmd.Dispose()
    ' cmd.Connection = cn
    'Me.Database1DataSet1.Clear()
     Me.StudentTBTableAdapter.Fill(Me.Database1DataSet1.StudentTB)
    '  'myda = New SqlDataAdapter(cmd)


End Sub

这是完整的代码

    Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlClient.SqlDataReader
Imports System.Windows.Forms

Public Class Student

    Dim cn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Nate\documents\visual studio 2012\Projects\WindowsApplication9\WindowsApplication9\Database1.mdf;Integrated Security=True")
    Dim cmd As New SqlCommand
    Dim dr As SqlDataReader
    Private dataAdapter As New SqlDataAdapter()
    Dim gen As String
    Dim bs As New BindingSource
    Dim dt As New DataTable

    Private Sub Student_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database1DataSet1.avTable' table. You can move, or remove it, as needed.
        Me.AvTableTableAdapter.Fill(Me.Database1DataSet1.avTable)
        'TODO: This line of code loads data into the 'Database1DataSet1.StudentTB' table. You can move, or remove it, as needed.
        Me.StudentTBTableAdapter.Fill(Me.Database1DataSet1.StudentTB)
        cmd.Connection = cn
        Form1.Hide()



    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If AvTableBindingSource.Position + 1 < AvTableBindingSource.Count Then
            AvTableBindingSource.MoveNext()
            Button7.PerformClick()
            ' Otherwise, move back to the first item.
        Else
            AvTableBindingSource.MoveFirst()
            Button7.PerformClick()
        End If

        ' Force the form to repaint.
        Me.Invalidate()



    End Sub


    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        PictureBox1.Image = System.Drawing.Bitmap.FromFile(ID.Text)
    End Sub


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        BDate1.Format = DateTimePickerFormat.Custom
        BDate1.CustomFormat = "MM/dd/yyyy"

        If fname.Text <> "" And lname.Text <> "" Then
            If Not cn.State = ConnectionState.Open Then
                cn.Open()
            End If
            ' cn1.Open()
            If rb1.Checked Then
                gen = rb1.Text.ToString
            ElseIf rb2.Checked Then
                gen = rb2.Text.ToString
            End If

            cmd.CommandText = "INSERT INTO StudentTB (FirstName,LastName,Birthday,Avatar,Gender,Grade) values('" & fname.Text & "', '" & lname.Text & "', '" & BDate1.Text & "', '" & AvtarNM.Text & "', '" & gen & "', '" & TxtGrade.Text & "')"
            cmd.ExecuteNonQuery()
            dt.Load(cmd.ExecuteReader())
            bs.DataSource = dt
            DataGridView1.DataSource = bs
            cn.Close()
            fname.Text = ""
            lname.Text = ""




        End If
    End Sub
    Private Sub genPCI()
        If rb1.Checked Then
            gen = rb1.Text.ToString
        ElseIf rb2.Checked Then
            gen = rb2.Text.ToString
        End If
    End Sub

    Public Function ChangeFormat(ByVal dtm As DateTime, ByVal format As String) As String
        Return dtm.ToString(format)
    End Function



    Private Sub Closestudent_Click(sender As Object, e As EventArgs) Handles Closestudent.Click
        Form1.Show()
        Me.Close()
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        bs.EndEdit()
        dataAdapter.Update(dt)
        DataGridView1.DataSource = dt

        ' dt.Load(Command.ExecuteReader())
        'Database1DataSet1.Reset()
        'DataGridView1.Columns.Clear()
        ' DataGridView1.DataSource = Nothing
        ' Me.StudentTBTableAdapter.Fill(Me.Database1DataSet1.StudentTB)
        ' StudentTBBindingSource.ResetBindings(True)
        ' Database1DataSet1.refresh()
        ' Dim myda As String
        '  cmd.Dispose()
        ' cmd.Connection = cn
        ' StudentTBBindingSource.ResetBindings(False)
        'Database1DataSet1.StudentTB.Clear()

        '  'myda = New SqlDataAdapter(cmd)

        ' DataGridView1.DataSource = Database1DataSet1.StudentTB
    End Sub


End Class
4

5 回答 5

1

你可以这样尝试

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    bs.EndEdit()
    dataadapter.Update(dt)
    DataGridView1.DataSource = dt

End Sub

更新

或者您可以使用 bindingdatasource 并在您的表单类中删除它

Dim bs As New BindingSource

并在 datagridview .. 在 button1_click

Dim dt As New DataTable
dt.Load(cmd.ExecuteReader())
bs.DataSource = dt
DataGridView1.DataSource = bs

所以,如果更新,那么将在 button4_click 之后显示

于 2013-05-26T08:45:36.577 回答
0

好吧,在玩了很多之后,这似乎解决了我的问题http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx好吧,我只能说它正在工作,不知道为什么,但它可以工作。

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    Me.DataGridView1.DataSource = Me.StudentTBBindingSource
    GetData("select * from StudentTB")

End Sub

Private Sub GetData(ByVal selectCommand As String)

    Try
        ' Specify a connection string. Replace the given value with a  
        ' valid connection string for a Northwind SQL Server sample 
        ' database accessible to your system. 
        Dim connectionString As String = "Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Nate\documents\visual studio 2012\Projects\WindowsApplication9\WindowsApplication9\Database1.mdf;Integrated Security=True"


        ' Create a new data adapter based on the specified query. 
        Me.dataAdapter = New SqlDataAdapter(selectCommand, connectionString)

        ' Create a command builder to generate SQL update, insert, and 
        ' delete commands based on selectCommand. These are used to 
        ' update the database. 
        Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)

        ' Populate a new data table and bind it to the BindingSource. 
        Dim table As New DataTable()
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        Me.dataAdapter.Fill(table)
        Me.StudentTBBindingSource.DataSource = table

        ' Resize the DataGridView columns to fit the newly loaded content. 
        ' Me.DataGridView1.AutoResizeColumns( _
        '  DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
    Catch ex As SqlException
        MessageBox.Show("To run this example, replace the value of the " + _
            "connectionString variable with a connection string that is " + _
            "valid for your system.")
    End Try

End Sub
于 2013-05-27T22:51:27.210 回答
0
    Datagridview1.datasource = nothing
Dataset.reset
dim cmd as new sqldataadapter
cmd = "SELECT * FROM YOUR_DATABSE", con
con.open
cmd.fill(Dataset)
datagridview1.datasource = dataset(0).defaultview

将这些代码添加到您的按钮插入

于 2013-05-27T01:32:37.437 回答
0

通过使用 ComboBox 进行搜索

sql = " select * from english_language where lang=' " & ComboBox2.Text & " ' "   

da = New OleDb.OleDbDataAdapter(sql, con)

Dim dt As New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
DataGridView1.Refresh()
于 2014-12-02T16:28:09.677 回答
0

试试这个代码来 btnClear。

DataGridView.DataSource = 0

按清除按钮。之后单击您编码的重新加载按钮。

我认为它将重置并重新加载数据网格视图中的数据。

于 2018-05-17T10:12:40.650 回答