刷新窗口表格gridview数据,vb.net 2008 [英] Refresh window form gridview data, vb.net 2008
问题描述
大家好,
我有一个窗口表单,可以将用户信息添加到数据库中,但是我不知道如何刷新与该表单相同的gridview数据.这是我要执行的操作,填写完用户信息后,单击添加用户"按钮,然后添加用户信息来执行数据库,并使用此新的用户信息刷新gridview.这可能吗?如果是这样,我该如何实现?我是否必须创建一个刷新按钮,以便可以单击它以使用此新用户信息刷新gridview?如果有的话,请给我这段代码来刷新gridview数据吗?在此方面的任何帮助将不胜感激!
非常感谢
这是将数据保存到数据库中的代码.
数据库名称= Test
表名称=用户
数据集名称= TestDataSet
Hello everyone,
I have a window form that allows me to add user info into the database, but I don''t know how to refresh the gridview data, which is on this same form. Here is what I want to do, After filling out the user info, I click Add User button and the user info is added to do the database and the gridview is refreshed with this new user info. Is this possible? If so, How can I achieve this? do I have to create a Refresh button so that it can be clicked to refresh the gridview with this new user info? Would you please give me this code for refreshing the gridview data if you have it? Any help on this is greatly appreciated!
Thank much
here is the code for saving data into database.
The database name = Test
Table name = Users
Dataset name = TestDataSet
Imports System.Data
Imports System.Data.SqlClient
Imports System.Globalization
Imports System.Windows.Forms.ControlBindingsCollection
Public Class Form1
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'TestDataSet.Users' table. You can move, or remove it, as needed.
Me.UsersTableAdapter.Fill(Me.TestDataSet.Users)
End Sub
Private Sub btnAddUser_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddUser.Click
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim UserName As String = Me.txtUserName.Text
Dim FirstName As String = Me.txtFirstName.Text
Dim LastName As String = Me.txtLastName.Text
If Me.txtUserName.Text = "" Then
MsgBox("You must enter a User Name." )
Exit Sub
End If
If Me.txtFirstName.Text = "" Then
MsgBox("You must enter First Name.")
Exit Sub
End If
If Me.txtLastName.Text = "" Then
MsgBox("You must enter Last Name.")
Exit Sub
End If
Try
con.ConnectionString = "Data Source =.; Initial Catalog = Test; Integrated Security = True;"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO [dbo].Users([UserName], [FirstName], [LastName]) VALUES('" & UserName & "','" & FirstName & "','" & LastName & "')"
cmd.ExecuteNonQuery()
MsgBox("Record inserted successfully")
Catch ex As Exception
MessageBox.Show("Error while inserting record on table...." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
End Sub
End Class
推荐答案
再次将DGV绑定到数据集.您要做的就是将DGV的DataSource属性设置为保存数据的表对象.
Bind the DGV to the dataset again. All you do is set the DataSource property of the DGV to your table object holding the data.
这篇关于刷新窗口表格gridview数据,vb.net 2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!