There is a class Func.cs , in it a function:

 public static void LoadTable(string table) { using (MySqlConnection con = new MySqlConnection("----------------------------")) { try { con.Open(); string sql = String.Format("SELECT * FROM `{0}`", table); using (MySqlCommand cmd = new MySqlCommand(sql, con)) { using (MySqlDataAdapter sda = new MySqlDataAdapter(cmd)) { using (DataTable dt = new DataTable()) { sda.Fill(dt); Form2 f = new Form2(); f.dataGridView1.DataSource = dt; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { con.Close(); } } } 

LoadTable call this:

 private void button1_Click(object sender, EventArgs e) { Func.LoadTable("proverka"); } 

As a result, the data in the dataGridView does not add.

If to take out this function from a class, then it works.

  • one
    using (DataTable dt = new DataTable ()) - here using does not need to be used, because dt is passed to DataGridView as a data source. Therefore, it is still too early to destroy it. - Uranus
  • one
    And why you inside using create the form? It seems to me that the object immediately dies after the creation of requests. - iluxa1810
  • 2
    Make a change to the question of where you are calling. It is possible with a description of the place (class). Then you will answer. And so it turns out the oil is oil , you have already written it - Denis Bubnov
  • one
    In general, this is the case: if you are in Form2, then you already have it and you can write this: this.dataGridView1.DataSource = dt; . If you have a class in which you want to do something, then during its initialization, transfer the form there and do what you need ... you just don’t provide enough code, we are just wondering how it is now. - Denis Bubnov
  • one
    @Maxim, I would walk through this place in the debager. 1) I would make sure that the new form successfully passes the initialization method of the components, and then recheck the assigned table. - iluxa1810

1 answer 1

And pass Form2 to your class, like this:

 private void button1_Click(object sender, EventArgs e) { Func.LoadTable(this, "proverka"); } 

Go to Form2.Designer.cs , find the line:

 private System.Windows.Forms.DataGridView dataGridView1; 

Change the access modifier from private to public - this will allow access to the component outside the form. (but this is not recommended). You can find the form designer like this (only you need to do this for Form2 according to your question):

enter image description here

Then correct your method:

 public static void LoadTable(Form2 form, string table) { // Ваш код... using (DataTable dt = new DataTable()) { sda.Fill(dt); form.dataGridView1.DataSource = dt; } // Ваш код... } 

I do not know how Func is created with you ... but oh well.

  • Thank! Bail out! And yes, I had a public modifier on dgv, so, the thing was that it was transferring data from a class incorrectly. Thank! =) - Max.
  • one
    @Maxim, pay attention to the fact that this is not recommended. In the sense of making the form components open. But if you really need, then ...) - Denis Bubnov