1

I'm setting Datagridview DataSource in Form1. In Form 2 I update those records (works fine in DB), and after updating them I want to refresh Datagridview in Form1 too.

Here is my code in Form1_Button_Click:

 OracleConn() 'connection to my DB

        Using cmd As New OracleCommand()

         Dim SQL As String = "Select * FROM MyTable"

            If Chk1.Checked = True Then

                cmd.Connection = OracleConn()
                cmd.CommandText = SQL
                cmd.CommandType = CommandType.Text

                Dim dt1 As New DataTable
                  Using dad As New OracleDataAdapter(SQL, OracleConn)
                    dad.Fill(dt1)
                  End Using
            End if
                DataGridView1.DataSource = dt1

End Using

How can I do this most easily ?

LuckyLuke82
  • 586
  • 1
  • 18
  • 58

2 Answers2

1

Write down a method to fill your data grid in Form1

public sub FillData()
OracleConn() 'connection to my DB

    Using cmd As New OracleCommand()

     Dim SQL As String = "Select * FROM MyTable"

        If Chk1.Checked = True Then

            cmd.Connection = OracleConn()
            cmd.CommandText = SQL
            cmd.CommandType = CommandType.Text

            Dim dt1 As New DataTable
              Using dad As New OracleDataAdapter(SQL, OracleConn)
                dad.Fill(dt1)
              End Using
        End if
            DataGridView1.DataSource = dt1

 End Using
 end sub

Call the method into your button click ( Form1_Button_Click)

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FillData() 
End sub

Now you can call the method of Form1 from Form2 directly after update,

Form1.FillData() 

Records will reflect in Form 1

NB: Method should be public in access.

vipin
  • 357
  • 3
  • 9
  • Actually a bad one, even tho it is working. Possible User selections will be lost, Rows arent actually updated, instead just completly new loaded. You might want to take a look into these 2 posts; http://stackoverflow.com/a/33702351/5686960 http://stackoverflow.com/a/40639530/5686960 – Luke Nov 17 '16 at 11:27
0

Instead of set

DataGridView1.DataSource = dt1;

Use a global BindingSource object, and bind it to both DataGridView, then all the changes made in one DataGrid will be reflected in the other.

BindingSource bsData = new BindingSource();
bsData.DataSource = dt1;

DataGridView1.DataSource = bsData;
DataGridView2.DataSource = bsData;

In your 2nd Form, bind the controls, TextBox, ComboBox, etc to this BindingSource and again, you don't need to refresh de DataGridView.

McNets
  • 10,352
  • 3
  • 32
  • 61
  • thanks for answer, but I used vipin solution since I dont have 2nd datagridview. I update records from Databinded textboxes. – LuckyLuke82 Nov 17 '16 at 10:37
  • It doesn't matter, you have a 2nd form with controls, bind this controls to de BindingSource and you don't need to refresh nothing at end – McNets Nov 17 '16 at 11:56
  • If I am allowed to make a guess; I think LuckyLuke82 has a second form for editing an entry in the DataGridView. All changes made are saved into the database and not on the client. That's why a global datasource wouldn't help in this case. Please correct me if I am wrong :-) – Luke Nov 17 '16 at 14:58
  • By default, using ADO.Net you're working on a disconnected dataset, until you call DataAdapter.Update() or DataAdapterManager.UpdateAll() methods. You can Add/Update/Delete record in your DataGrid, and then Commit or Reject all changes. – McNets Nov 17 '16 at 15:05
  • @Luke, you're right, I make changes directly to Oracle and afterwards I refresh Datagridview which is visible too, but located in different form (which is opened in Tab control). – LuckyLuke82 Nov 18 '16 at 09:07
  • @mcNets, I tried your suggestion too, but It doesn't work....Maybe I just fixed It wrong or some obstacle in code which I don't see. Thanks for reply ! – LuckyLuke82 Nov 18 '16 at 09:09