Results 1 to 15 of 15

Thread: [RESOLVED]Refreshing A DataGrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Thumbs up [RESOLVED]Refreshing A DataGrid

    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
    Last edited by Roofuss; Apr 22nd, 2009 at 06:05 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Refreshing A DataGrid

    There should be no need to refresh anything. The data should be added to the grid first, then saved. First, you populate a DataTable and bind it to your grid to display the data. When the user adds a new record you should be adding a new row to that DataTable first, which will automatically show up in the bound grid. You then save the changes in the DataTable back to the database using the very same TableAdapter that you populated it with in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Refreshing A DataGrid

    Quote Originally Posted by jmcilhinney View Post
    There should be no need to refresh anything. The data should be added to the grid first, then saved. First, you populate a DataTable and bind it to your grid to display the data. When the user adds a new record you should be adding a new row to that DataTable first, which will automatically show up in the bound grid. You then save the changes in the DataTable back to the database using the very same TableAdapter that you populated it with in the first place.
    Hi Thanks for the reply

    I think i understand however when i am adding a new customer i am not adding it directly onto the grid im adding it through my my addCustomer form. all the grid does is display the the information i have added. however at the moment the only way i can see new entries is if i close the program down and start it again because the information for the datagrid gets imported at load time. i want it to appear straight after i have close my addCustomer form.

    Any ideas?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Refreshing A DataGrid

    There's no reason your user interface has to change. Your main form displays the grid. The user clicks a button or whatever and a dialogue is displayed where they enter the data for a new record. When that dialogue closes the main form retrieves the data from the dialogue and adds it to the data that it's displaying in the grid, which it then saves. The user sees exactly the same workflow but your application follows the "proper" way of doing things.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Refreshing A DataGrid

    Quote Originally Posted by jmcilhinney View Post
    There's no reason your user interface has to change. Your main form displays the grid. The user clicks a button or whatever and a dialogue is displayed where they enter the data for a new record. When that dialogue closes the main form retrieves the data from the dialogue and adds it to the data that it's displaying in the grid, which it then saves. The user sees exactly the same workflow but your application follows the "proper" way of doing things.
    Thanks so would how i achieve this? would you simply write a query to update the table upon closing the dialog? if so how? im a massive time noob to sql

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Refreshing A DataGrid

    You use the very same TableAdapter you used to get the data in the first place. You simply call Update instead of Fill. You can do that each time the user adds a record if you need to but once when you close the grid form is likely enough.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Refreshing A DataGrid

    Quote Originally Posted by jmcilhinney View Post
    You use the very same TableAdapter you used to get the data in the first place. You simply call Update instead of Fill. You can do that each time the user adds a record if you need to but once when you close the grid form is likely enough.
    Sorry i tried that and its still not working is says that:

    Me.CustomersTableAdapter.Update(Me.MySoftwareDB.customers) does not belong to my dialog form i've tried to call it as a sub but still doesnt work?

    if you were going to put it on my form below how would you write it?

    Code:
    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
    any ideas this is really stressing me out lol

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Refreshing A DataGrid

    As I've already said, you don't save the data in the dialogue. You save it in the main form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Refreshing A DataGrid

    so are you saying to take the code from my save button and place it in my main form then call the procedure in my save button?

  10. #10
    Member
    Join Date
    Mar 2009
    Posts
    39

    Re: Refreshing A DataGrid

    This may help,

    http://www.vbforums.com/showthread.php?t=561829

    Also, it may cause you issues using a querey to insert the data into the backend, you should let the datagrid take the info and insert it, it may cause redundency errors.

  11. #11
    Member
    Join Date
    Mar 2009
    Posts
    39

    Re: Refreshing A DataGrid

    This code is placed in the "Main Form" on a toolstrip label:

    Code:
    Private Sub ToolStripLabelProc_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripLabelProc.Click
    
            If AddProc.ShowDialog() = Windows.Forms.DialogResult.OK Then
                With ProcessorComboBox
                    .DisplayMember = "Processor"
                    .ValueMember = "Processor"
                    .DataSource = GetList("Processors", "Processor")
                End With
            End If
        End Sub
    Upon clicking that label it opens another form to allow the client to enter a new proc type, the following code is in the new form inside the 2 command buttons on the form:

    Code:
    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
    
            Me.DialogResult = Windows.Forms.DialogResult.OK
    
        End Sub
    
        Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
    
            Me.DialogResult = Windows.Forms.DialogResult.Cancel
    
        End Sub

  12. #12
    New Member
    Join Date
    Jan 2008
    Posts
    6

    Re: Refreshing A DataGrid

    Quote Originally Posted by jmcilhinney View Post
    There's no reason your user interface has to change. Your main form displays the grid. The user clicks a button or whatever and a dialogue is displayed where they enter the data for a new record. When that dialogue closes the main form retrieves the data from the dialogue and adds it to the data that it's displaying in the grid, which it then saves. The user sees exactly the same workflow but your application follows the "proper" way of doing things.

    Roofuss

    The best way is like jmcilhinney is suggesting, but what you are doing. It might work if you put the code that you are using to fill your datagrid in your form_load event, in the form_activated event instead, it will trigger when you go back to your main form with the datagrid. I believe a simply fix, but not the best way of doing things! If I understand your first post that the datagrid is populated in the forms_load event.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Refreshing A DataGrid

    Quote Originally Posted by N2DFIRE View Post
    Roofuss

    The best way is like jmcilhinney is suggesting, but what you are doing. It might work if you put the code that you are using to fill your datagrid in your form_load event, in the form_activated event instead, it will trigger when you go back to your main form with the datagrid. I believe a simply fix, but not the best way of doing things! If I understand your first post that the datagrid is populated in the forms_load event.
    sorry guys thats not working either i think it must be the way ive got my forms set up and maybe im not using the binding controls to there full use or its probs just me and i need to get my head around it more, im new to sql and havent fully explored it yet ill keep plugging away and hopefully will get this resolved. thanks for all your posted and help though i think ill lay it all out as you both surgested.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Refreshing A DataGrid

    These are the steps you should take:

    1. Open main form.
    2. Load data into DataTable and bind to DataGridView.
    3. Open dialogue.
    4. User enters data.
    5. Close dialogue.
    6. Main form gets data from dialogue.
    7. Main form creates new row and adds to DataTable, which inherently updates the bound grid.
    8. Main form saves contents of Datatable to database.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    115

    Re: Refreshing A DataGrid

    Thanks ive followed your steps and....... IT WORKS!! thanks for all your help it works a treat now

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width