vb.net Data Gridview REfresh

时间:2013-05-26 06:07:06

标签: vb.net

我在更新我在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

5 个答案:

答案 0 :(得分: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并对其进行delare
Dim bs As New BindingSource

并在datagridview ..中按钮1_click

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

因此,如果更新,则会在button4_click

之后显示

答案 1 :(得分: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

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

答案 2 :(得分: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

答案 3 :(得分: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()

答案 4 :(得分:0)

尝试使用此代码来btnClear。

DataGridView.DataSource = 0

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

我认为它会在数据网格视图中重置和重新加载数据。