Click here to Skip to main content
16,014,294 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


VB
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
Posted
Updated 5-Jul-12 7:03am
v2

1 solution

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.
 
Share this answer
 
Comments
hdng76 6-Jul-12 10:21am    
Hi Dave,
Thanks very much for your golden advice!
Would you please be more specific as I am just a beginner?
Thanks again!
Dave Kreskowiak 6-Jul-12 16:04pm    
Whoops. I just noticed you're using the designer methods for handling data (Yuck!).

After you Execute your INSERT command, you'll have to call the Fill method on your UserTableAdapter again. If the DGV doesn't update, then you just set it's DataSource property to your TestDataSet.Users datatable.

myDataGridView.DataSource = TestDataSet.Users
hdng76 13-Jul-12 9:31am    
Hi Dave,
I called this Fill method again and it worked as expected! Thanks very much for all of your help! It's greatly appreciated!
UsersTableAdapter.Fill(Me.TestDataSet.Users)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900