Question Refreshing a DataGridview?

Roofuss

Member
Joined
Apr 19, 2009
Messages
9
Programming Experience
Beginner
Hi

I have my main form which has a "New Customer" button on it and a DataGrid

when you click the "New Customer" Button it opens up a new form where you can add all the detials in and press save then the data is saved into SQL database.

can anyone tell me how to refresh my datagrid upon closing this form? as the as at the moment the data from my SQL database is loaded in when the form loads(program starts) using the code below

CODE]Me.CustomersTableAdapter.Fill(Me.MySoftwareDB.customers)[/CODE]

Thanks
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
this shouldnt be necessary if you are using the same dataset and not creating an new one between forms everything should be seen automatically.
 

Roofuss

Member
Joined
Apr 19, 2009
Messages
9
Programming Experience
Beginner
this shouldnt be necessary if you are using the same dataset and not creating an new one between forms everything should be seen automatically.

See thats what i thought but it doesnt seem to be working ive tried everything i can think of and nothing is helping me. please see the code for my save button below

VB.NET:
Imports System.Data.SqlClient
Imports System.Data



Public Class frmCustomer
    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        'GETS COONECTION STRING FROM THE MSDN FORM/CLASS
        Dim Connection As SqlConnection = MSDB.GetConnection
        'DOES THE CONNECTION TO THE SQL DATABASE
        Dim mySqlCommand As SqlCommand = New SqlCommand
        Connection.Open()
        ' WHILE THE CONNECTION IS OPEN
        mySqlCommand.Connection = Connection
        'INSERT THE FOLLOWING DETAILS FROM THE TEXT BOXES ON THE CURRENT FORM INTO THE CUSTOMER TABLE  IN THE SQLDATABASE CALLED MYSOFTWAREDB
        Try
            mySqlCommand.CommandText = "INSERT INTO customers (CustomerID, Customer, Contact, Email) VALUES (@CustomerID, @Customer, @Contact, @Email)"
            mySqlCommand.Parameters.AddWithValue("@CustomerID", tbCustomerID.Text)
            mySqlCommand.Parameters.AddWithValue("@Customer", tbCustomer.Text)
            mySqlCommand.Parameters.AddWithValue("@Contact", tbContact.Text)
            mySqlCommand.Parameters.AddWithValue("@Email", tbEmail.Text)
            mySqlCommand.ExecuteNonQuery()
            'IF THE QUERY IS SUCCESSFULL THEN SHOW MESSAGE BOX BELOW
            MessageBox.Show("New Customer Added and Database Updated.", "Accepted", MessageBoxButtons.OK, MessageBoxIcon.Information)
            'IF THE QUERY IS NOT SUCCESSFULL THEN SHOW THIS MESSAGE BOX BEOW THAT CONTAINS THE ERROR MESSAGE
        Catch ex As Exception
            MessageBox.Show("Customer could not be added: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            'FINALLY CLOSE THE CONNECTION IF ITS STILL OPEN 
        Finally
            If Connection.State = ConnectionState.Open Then
                Connection.Close()
            End If
        End Try
        'THEN CLOSE THE FORM AND RETURN TO FRMMAIN.
        Close()
    End Sub

End Class

thanks
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
I'm not questioniong your save code, you would know if the results are not being saved according to whether the results wind up in your database or not. Additional note: intResults = mySqlCommand.NonQuery would hold the number of records inserted to check for when you call your messagebox.

What I am questioning is where your dataset is declared and bound on both forms... I think instead of using one global declared dataset you are using two form level datasets which are not seeing the results between each.
 

Roofuss

Member
Joined
Apr 19, 2009
Messages
9
Programming Experience
Beginner
you right its only bound to one form which is my main form which loads the data into the datagrid on form_load. how do i also make it bound to my add customer form so that when the data is saved it automatically updates my datagrid like you say?
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
Im assuming what you have bound to your datagridview is a dataset or datatable declared in your one form. You would instead need to declare that dataset or datatable globaly so that all forms can see that dataset. In the form you are creating a new record, you then add the record right to your dataset and then calling the dataadapter.update method to send it to the db.

Other then that you would need to refill your dataset on the parent form when the child form closes.
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
He still needs to see the data between different forms, so how would this be replaced by a binding source?
 

Tom

Well-known member
Joined
Aug 23, 2005
Messages
746
Programming Experience
5-10
It certainly would work but not sure where its any easier... In the one case your passing your source as a parameter to the new form and in the other its being declared gobally and automatically seen by both.
 
Top Bottom