用新数据重新加载datagridview [英] reloading datagridview with new data

查看:396
本文介绍了用新数据重新加载datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我是设置我的测试程序,如果您在文本框中输入文本并点击添加,它将添加信息 到数据库.如果我点击清除,它将清除datagridview中的窗口.当我点击reload时,它只是重新加载旧数据,而不是所做的更改.当我退出程序并重新运行新数据时.
在我的表单我有一个Datagridview1和4个文本框和4个botton.这些按钮是(添加,删除,清除,重新加载) 我有Database1DataSet,TableBindingSource,TableTableAdapter

Hello all,

I am setting up my test program where if you enter in text into the textboxes and hit add it will add the information to the database. If I hit clear it clears out the window in the datagridview. When I hit reload, it just reloads the old data, not the changes that were made. When I exit the program and re run the new data is there.

On my form I have a Datagridview1 and 4 textboxes and 4 bottons. The buttons are (Add,Delete,Clear,Reload) I have Database1DataSet, TableBindingSource, TableTableAdapter

我有3个按钮(添加,删除,清除)正在工作.
感谢任何帮助.我正在拔头发试图解决这个问题.

I have the 3 buttons (Add,Delete,Clear) working. But I can not get the Reload button to work.
Thanks for any help. I am pulling my hair out trying to figure this out.

Imports System.Data.SqlClient
Public Class Form1
    Dim myconnection As SqlConnection
    Dim mycommand As SqlCommand
    Dim dr As SqlDataReader
    Dim dr1 As SqlDataReader
    
    Public imagechange As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        myconnection = New SqlConnection("Data Source=(LocalDB)\v11.0 ; AttachDbFilename='C:\Users\Brian\Documents\visual studio 2013\Projects\WindowsApplication2\WindowsApplication2\Database1.mdf' ; Initial Catalog=Database1.mdf;Integrated Security=True")
        Me.TableTableAdapter.Fill(Me.Database1DataSet.Table)
    End Sub
    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles add.Click
        myconnection.Open()
        mycommand = New SqlCommand("insert into dbo.[Table] ([Id],[PlanetX],[PlanetY],[PlanetName]) values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')", myconnection)
        mycommand.ExecuteNonQuery()

        MessageBox.Show("New Row Inserted")
        myconnection.Close()
        MsgBox("Succesfull")

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles delete.Click
        myconnection.Open()
        mycommand = New SqlCommand("delete from dbo.[Table] WHERE [Id] = ('" & TextBox1.Text & "')", myconnection)
        mycommand.ExecuteNonQuery()

        MessageBox.Show("New Row Deleted")
        myconnection.Close()
        MsgBox("Succesfull")

    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Clear.Click
        Database1DataSet.Clear()
    End Sub
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles reload.Click
        DataGridView1.DataSource = Nothing
        DataGridView1.DataSource = TableBindingSource
        Me.TableTableAdapter.Fill(Me.Database1DataSet.Table)
    End Sub
End Class

推荐答案

你好,

我建议您查看我的MSDN文章将新记录添加到SQL-Server表并进行更新实时的DataGridView .在文章的代码中,我一次加载,您 可以以相同的方式重新加载,添加数据是添加行并返回新的自动增量主键的最佳实践.我没有显示删除内容,但是通过遵循其中的代码,找出删除内容并不困难.

I would suggest looking at my MSDN article Adding new records into SQL-Server table and update a DataGridView in real time. In the code for the article I do one load, you could re-load the same way, adding data is best practice for adding a row and returning the new auto-increment primary key. I don't show a delete but by following the code within it is not difficult to figure out the delete.


这篇关于用新数据重新加载datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆