datagrid 重载本地数据,用新数据重新加载datagridview

博主在测试程序中遇到 Datagridview 重载数据问题,点击重载按钮只能加载旧数据,无法加载更改后的数据。博主展示了相关代码,包括添加、删除、清空和重载按钮的功能实现。最后给出解决方案,建议参考 MSDN 文章实时更新 DataGridView。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

6c037831acdea310a819c72c58331a07.png

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

解决方案Hello,

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.

304737347fd93485eba01f0dcfb0bdfd.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值