Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. refresh datagrid on another windows form

refresh datagrid on another windows form

Scheduled Pinned Locked Moved Visual Basic
databaseannouncement
8 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sohaib_a
    wrote on last edited by
    #1

    I have two forms in my application.On form1 there is datagridview -dg1 which is filled with a dataset-ds1. On form2 I have a some code ,which after fulfilling certain conditions updates the database table which is contained in ds1. When this is done I want to refresh datagridview-dg1 and show the updated table. Both the forms are already open and I have to update them.

    Form2

    Public Event refreshdg1(ByVal st As String)

    Private Sub

    .......Some condition -->True then
    ....... Update database table

    'I tried three ways here

    1- Raiseevent refreshdg1(s1)

    2-

        Dim StrCon As String
        Dim conn As SqlConnection
        StrCon = ConfigurationManager.AppSettings("StrCon")
        conn = New SqlConnection(StrCon)
        conn.Open()
       Dim ds As New SqlDataAdapter("Select \* from Tag\_History where Tag\_ID = '" + s1 + "' ", conn)
      Dim commandBuilder As New SqlCommandBuilder(listadapter)
    
      Dim table As New DataTable()
    
      table.Locale = System.Globalization.CultureInfo.InvariantCulture
     listadapter.Fill(table)
    
     Form1.dg1.DataSource = table
     conn.Close()
    

    3- Form1.refreshdg(s1)

    End sub

    Form1:

    Addhandler Form2.(I cant refreshdg1 here)

    Private Sub refreshdg(ByVal s As String)
    Dim f As New Parking_control
    AddHandler f.refreshdg1, AddressOf refreshdg
    Dim StrCon As String
    Dim conn As SqlConnection
    StrCon = ConfigurationManager.AppSettings("StrCon")
    conn = New SqlConnection(StrCon)
    conn.Open()

        Dim person As String
        person = s
    
        Dim ds1 As New SqlDataAdapter("Select \* from Tag\_History where Name = '" + person + "' ", conn)
        Dim commandBuilder As New SqlCommandBuilder(ds1)
    
    
        Dim table As New DataTable()
    
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        listadapter1.Fill(table)
        table.Columns(0).ColumnName = "Tag ID"
        table.Columns(1).ColumnName = "Date   Time"
        table.Columns(3).ColumnName = "Tag Status"
        dg1.DataSource = table
    
        conn.Close()
    
    End Sub
    
    L C A 3 Replies Last reply
    0
    • S sohaib_a

      I have two forms in my application.On form1 there is datagridview -dg1 which is filled with a dataset-ds1. On form2 I have a some code ,which after fulfilling certain conditions updates the database table which is contained in ds1. When this is done I want to refresh datagridview-dg1 and show the updated table. Both the forms are already open and I have to update them.

      Form2

      Public Event refreshdg1(ByVal st As String)

      Private Sub

      .......Some condition -->True then
      ....... Update database table

      'I tried three ways here

      1- Raiseevent refreshdg1(s1)

      2-

          Dim StrCon As String
          Dim conn As SqlConnection
          StrCon = ConfigurationManager.AppSettings("StrCon")
          conn = New SqlConnection(StrCon)
          conn.Open()
         Dim ds As New SqlDataAdapter("Select \* from Tag\_History where Tag\_ID = '" + s1 + "' ", conn)
        Dim commandBuilder As New SqlCommandBuilder(listadapter)
      
        Dim table As New DataTable()
      
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
       listadapter.Fill(table)
      
       Form1.dg1.DataSource = table
       conn.Close()
      

      3- Form1.refreshdg(s1)

      End sub

      Form1:

      Addhandler Form2.(I cant refreshdg1 here)

      Private Sub refreshdg(ByVal s As String)
      Dim f As New Parking_control
      AddHandler f.refreshdg1, AddressOf refreshdg
      Dim StrCon As String
      Dim conn As SqlConnection
      StrCon = ConfigurationManager.AppSettings("StrCon")
      conn = New SqlConnection(StrCon)
      conn.Open()

          Dim person As String
          person = s
      
          Dim ds1 As New SqlDataAdapter("Select \* from Tag\_History where Name = '" + person + "' ", conn)
          Dim commandBuilder As New SqlCommandBuilder(ds1)
      
      
          Dim table As New DataTable()
      
          table.Locale = System.Globalization.CultureInfo.InvariantCulture
          listadapter1.Fill(table)
          table.Columns(0).ColumnName = "Tag ID"
          table.Columns(1).ColumnName = "Date   Time"
          table.Columns(3).ColumnName = "Tag Status"
          dg1.DataSource = table
      
          conn.Close()
      
      End Sub
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You can use Eventheandler for Refresing DataGrid.

      If you can think then I Can.

      S 1 Reply Last reply
      0
      • S sohaib_a

        I have two forms in my application.On form1 there is datagridview -dg1 which is filled with a dataset-ds1. On form2 I have a some code ,which after fulfilling certain conditions updates the database table which is contained in ds1. When this is done I want to refresh datagridview-dg1 and show the updated table. Both the forms are already open and I have to update them.

        Form2

        Public Event refreshdg1(ByVal st As String)

        Private Sub

        .......Some condition -->True then
        ....... Update database table

        'I tried three ways here

        1- Raiseevent refreshdg1(s1)

        2-

            Dim StrCon As String
            Dim conn As SqlConnection
            StrCon = ConfigurationManager.AppSettings("StrCon")
            conn = New SqlConnection(StrCon)
            conn.Open()
           Dim ds As New SqlDataAdapter("Select \* from Tag\_History where Tag\_ID = '" + s1 + "' ", conn)
          Dim commandBuilder As New SqlCommandBuilder(listadapter)
        
          Dim table As New DataTable()
        
          table.Locale = System.Globalization.CultureInfo.InvariantCulture
         listadapter.Fill(table)
        
         Form1.dg1.DataSource = table
         conn.Close()
        

        3- Form1.refreshdg(s1)

        End sub

        Form1:

        Addhandler Form2.(I cant refreshdg1 here)

        Private Sub refreshdg(ByVal s As String)
        Dim f As New Parking_control
        AddHandler f.refreshdg1, AddressOf refreshdg
        Dim StrCon As String
        Dim conn As SqlConnection
        StrCon = ConfigurationManager.AppSettings("StrCon")
        conn = New SqlConnection(StrCon)
        conn.Open()

            Dim person As String
            person = s
        
            Dim ds1 As New SqlDataAdapter("Select \* from Tag\_History where Name = '" + person + "' ", conn)
            Dim commandBuilder As New SqlCommandBuilder(ds1)
        
        
            Dim table As New DataTable()
        
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            listadapter1.Fill(table)
            table.Columns(0).ColumnName = "Tag ID"
            table.Columns(1).ColumnName = "Date   Time"
            table.Columns(3).ColumnName = "Tag Status"
            dg1.DataSource = table
        
            conn.Close()
        
        End Sub
        
        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        Wow - what horrible code. Also, depending on where 's' comes from, odds are good that I could erase your database just by using the search functionality. The answer to your original question is delegates, but I'd say you have a long way to go in general before you can write code anyone would want to run, let alone pay for.

        Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

        A 1 Reply Last reply
        0
        • L Lost User

          You can use Eventheandler for Refresing DataGrid.

          If you can think then I Can.

          S Offline
          S Offline
          sohaib_a
          wrote on last edited by
          #4

          how..what do you mean..haven't i used that already?

          C 1 Reply Last reply
          0
          • S sohaib_a

            how..what do you mean..haven't i used that already?

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            He means set up a delegate that is called in form2 and is hooked to a method in form1

            Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

            1 Reply Last reply
            0
            • S sohaib_a

              I have two forms in my application.On form1 there is datagridview -dg1 which is filled with a dataset-ds1. On form2 I have a some code ,which after fulfilling certain conditions updates the database table which is contained in ds1. When this is done I want to refresh datagridview-dg1 and show the updated table. Both the forms are already open and I have to update them.

              Form2

              Public Event refreshdg1(ByVal st As String)

              Private Sub

              .......Some condition -->True then
              ....... Update database table

              'I tried three ways here

              1- Raiseevent refreshdg1(s1)

              2-

                  Dim StrCon As String
                  Dim conn As SqlConnection
                  StrCon = ConfigurationManager.AppSettings("StrCon")
                  conn = New SqlConnection(StrCon)
                  conn.Open()
                 Dim ds As New SqlDataAdapter("Select \* from Tag\_History where Tag\_ID = '" + s1 + "' ", conn)
                Dim commandBuilder As New SqlCommandBuilder(listadapter)
              
                Dim table As New DataTable()
              
                table.Locale = System.Globalization.CultureInfo.InvariantCulture
               listadapter.Fill(table)
              
               Form1.dg1.DataSource = table
               conn.Close()
              

              3- Form1.refreshdg(s1)

              End sub

              Form1:

              Addhandler Form2.(I cant refreshdg1 here)

              Private Sub refreshdg(ByVal s As String)
              Dim f As New Parking_control
              AddHandler f.refreshdg1, AddressOf refreshdg
              Dim StrCon As String
              Dim conn As SqlConnection
              StrCon = ConfigurationManager.AppSettings("StrCon")
              conn = New SqlConnection(StrCon)
              conn.Open()

                  Dim person As String
                  person = s
              
                  Dim ds1 As New SqlDataAdapter("Select \* from Tag\_History where Name = '" + person + "' ", conn)
                  Dim commandBuilder As New SqlCommandBuilder(ds1)
              
              
                  Dim table As New DataTable()
              
                  table.Locale = System.Globalization.CultureInfo.InvariantCulture
                  listadapter1.Fill(table)
                  table.Columns(0).ColumnName = "Tag ID"
                  table.Columns(1).ColumnName = "Date   Time"
                  table.Columns(3).ColumnName = "Tag Status"
                  dg1.DataSource = table
              
                  conn.Close()
              
              End Sub
              
              A Offline
              A Offline
              Andy Missico
              wrote on last edited by
              #6

              This is a case where you do not need an event. Events are good when one or more objects need to react to another object's behavior. Here one form is telling the other form to update. Therefore, you just need a simple method call. Event handling just complicates the communication between the two forms (objects). Remove all event handling code, such as the Public Event, AddHandler, and RaiseEvent statements. Just call Form1.refreshdg(s1) as you did in Item #3. I usually use RefreshUI as the standard entry point for my forms. If needed, I will add RefreshData, RefreshButtonState, and so on.

              S 1 Reply Last reply
              0
              • C Christian Graus

                Wow - what horrible code. Also, depending on where 's' comes from, odds are good that I could erase your database just by using the search functionality. The answer to your original question is delegates, but I'd say you have a long way to go in general before you can write code anyone would want to run, let alone pay for.

                Christian Graus Driven to the arms of OSX by Vista. "I am new to programming world. I have been learning c# for about past four weeks. I am quite acquainted with the fundamentals of c#. Now I have to work on a project which converts given flat files to XML using the XML serialization method" - SK64 ( but the forums have stuff like this posted every day )

                A Offline
                A Offline
                Andy Missico
                wrote on last edited by
                #7

                Why are you being rude? Please remove your comment.

                1 Reply Last reply
                0
                • A Andy Missico

                  This is a case where you do not need an event. Events are good when one or more objects need to react to another object's behavior. Here one form is telling the other form to update. Therefore, you just need a simple method call. Event handling just complicates the communication between the two forms (objects). Remove all event handling code, such as the Public Event, AddHandler, and RaiseEvent statements. Just call Form1.refreshdg(s1) as you did in Item #3. I usually use RefreshUI as the standard entry point for my forms. If needed, I will add RefreshData, RefreshButtonState, and so on.

                  S Offline
                  S Offline
                  sohaib_a
                  wrote on last edited by
                  #8

                  Thanks...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups