Results 1 to 2 of 2

Thread: attach code to DataGridView

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    attach code to DataGridView

    I have a DataGridView setup on my form that connects to an Access database. When new data is entered in the DataGridView, the Access database is not automatically updated. I wrote some code that updates the access database by clicking a button. I'd like to attached that code every time I change rows on the DataGridView. Pressing a button every time to update is a hassle and sometime you forget. How do I do that?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: attach code to DataGridView

    Take a look at the DataGridView_RowsAdded Event. I would put your code that you use in your update button into a sub and call it when needed, like so:
    Code:
    Private Sub updateCode()
            MessageBox.Show("I just used my update sub")
        End Sub
    
        Private Sub DataGridView1_RowsAdded(sender As Object, e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
            Call updateCode()
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Call updateCode()
        End Sub
    Edit - Mind you, you don't have to use Call in "Call updateCode()". I do that outta habbit, it's just easier for me to read.
    Last edited by dday9; Jun 20th, 2012 at 11:41 AM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Tags for this Thread

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