Hello!

If anyone came across this, tell dataGridView how to update the dataGridView without closing the form. Here is the program code. The user enters the name in the text field, clicks the Browse button and select an image. Click the Add button and add it all to the database. To display the data in the dataGridView , you must first close the form, then open. How to make the form not to close, but to click on the Refresh button and the data just entered is displayed in the dataGridView . dataGridView1.Refresh() and dataGridView1.Update() do not update the data. Tell me.

Here is the code:

 public partial class Form1 : Form { string pathImageBrand = string.Empty; public Form1() { InitializeComponent(); } //ΠΊΠ½ΠΎΠΏΠΊΠ° Browse private void button1_Click(object sender, EventArgs e) { openFileDialog1.Filter = "Images (*.jpg; *.jpeg; *.gif; *.bmp; *.ico; *.png) | *.jpg; *.jpeg; *.gif; *.bmp; *.ico; *.png"; if(openFileDialog1.ShowDialog() == DialogResult.OK) { pathImageBrand = openFileDialog1.FileName.ToString(); } } //ΠΊΠ½ΠΎΠΏΠΊΠ° Add private void button2_Click(object sender, EventArgs e) { string stringConnect = @"server=localhost;user id=root;password=12345;database=abc"; string sql = "INSERT INTO avto VALUES('"+textBox1.Text+"', @imgBrands)"; byte[] imgBrands = null; FileStream fsBrand = new FileStream(pathImageBrand, FileMode.Open, FileAccess.Read); BinaryReader brBrand = new BinaryReader(fsBrand); imgBrands = brBrand.ReadBytes((int)fsBrand.Length); using(MySqlConnection connect = new MySqlConnection(stringConnect)) { connect.Open(); MySqlCommand command = new MySqlCommand(sql, connect); command.Parameters.Add(new MySqlParameter("@imgBrands", imgBrands)); int x = command.ExecuteNonQuery(); MessageBox.Show(x.ToString() + "record(s) saved"); } textBox1.Clear(); } //ΠΊΠ½ΠΎΠΏΠΊΠ° Refresh private void button3_Click(object sender, EventArgs e) { //ΠΎΠ±Π½ΠΎΠ²ΠΈΡ‚ΡŒ Ρ‚Π°Π±Π»ΠΈΡ†Ρƒ Π½Π΅ закрывая, Π° Π·Π°Ρ‚Π΅ΠΌ открывая Ρ„ΠΎΡ€ΠΌΡƒ //dataGridView1.Update(); dataGridView1.Refresh(); } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'abcDataSet.avto' table. You can move, or remove it, as needed. this.avtoTableAdapter.Fill(this.abcDataSet.avto); } } 
  • Do you read the database in Button_Click ? And no one complained that your program freezes? - VladD
  • @ Anton121212, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina
  • Where do you see a comprehensive answer here? He's not here, sheer la-la ... - Anton121212
  • // button Refresh private void button3_Click (object sender, EventArgs e) {// update the table without closing and then opening the form this.avtoTableAdapter.Fill (this.abcDataSet.avto); } - Anton121212

3 answers 3

Do you want the data to abcDataSet.avto to abcDataSet.avto some miracle? In the button2_Click handler button2_Click you are adding directly to the database, and abcDataSet.avto do not update abcDataSet.avto after that. Since we started using the SqlDataAdapter , then use it and perform all the interaction with the database. And if you want to leave it as it is, then after adding the data to the database, update abcDataSet.avto with your hands.

    Re-upload data.

      In my case, a new window was called from the main form, to add data to the MS SQL database, but after adding and closing the auxiliary window, DataGridView itself was not updated ... I spent several hours searching for a solution, and was very surprised when the brilliant solution was found to update the DataSet. Update example attached -

       private void MainForm_Activated(object sender, EventArgs e) { this.clientsTableAdapter.Fill(this.onlineStoreDataSet.Clients); }