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); } }
Button_Click? And no one complained that your program freezes? - VladD