Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb.net Data Gridview REfresh

Tags:

vb.net

i am having some issues updating a table that i have in a VB.net Windows Forms in VS 2012

i have setup the database from a service Data base, it just need a small amount of data stored locally

what i am trying to do when i create a new user it update the data grid view

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 = ""

the issue is that i can clear the table and reload it with button but it does not show the updated values with out closing the application then reopening it, i have tried a few thing and nothing seems to refresh the connection and or update the database. any help would help.

thanks

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

here is the full code

    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
like image 368
OneNathan Avatar asked Mar 26 '26 06:03

OneNathan


1 Answers

You can try like this

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

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

End Sub

UPDATE

or you can use bindingdatasource and delare it on your form class

Dim bs As New BindingSource

and in datagridview .. in button1_click

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

So, if updated then will show after button4_click

like image 182
matzone Avatar answered Mar 27 '26 22:03

matzone



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!