C # update DataGridView when updating or pasting into another form

I have 2 forms that are form A and form B ,

form A allows the user to insert and update student information.

form B is just a DataGridView and the button there.

When I insert a student in form A , I go to form B , the new student does not appear in the DataGridView, and if I re-run the program, the new student will appear in form B

I tried using this button on form b

 datagridview1.refresh(); datagridview1.update(); 

but it still does not work.


Edited by:

My code to insert a worker

 cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,[Position],Photo) values('" + textBox5.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Value + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + dateTimePicker2.Value + "',@Position,@Photo)", con); cmd.Parameters.AddWithValue("@Position", comboBox1.SelectedText.ToString()); conv_photo(); con.Open(); int n = cmd.ExecuteNonQuery(); //cmd.ExecuteNonQuery(); con.Close(); if (n > 0) { MessageBox.Show("Inserted"); loaddata(); rno++; } else MessageBox.Show("No Insert"); } 

my Datagridview1(Form2) does not update automatically when I inserted a new worker. But if I re-run the application, a new worker will appear.

+7
source share
6 answers
 // Form A public void loaddata() { //do what you do in load data in order to update data in datagrid } 

then on form B define:

 // Form B FormA obj = (FormA)Application.OpenForms["FormA"]; private void button1_Click(object sender, EventArgs e) { obj.loaddata(); datagridview1.Update(); datagridview1.Refresh(); } 
+10
source

DataGridView.Refresh and DataGridView.Update methods that are inherited from Control. They are associated with redrawing the control, so new lines are not displayed.

I assume the data search is on Form_Load. If you want your button in Form B to retrieve the latest data from the database, then you must do what Form_Load does.

A good way to do this is to separate the data retrieval calls into a separate function and call it from both the From Load and Click Click events.

+2
source

quick example should be a sufficient starting point

Code A Form

 public event EventHandler<EventArgs> RowAdded; private void btnRowAdded_Click(object sender, EventArgs e) { // insert data // if successful raise event OnRowAddedEvent(); } private void OnRowAddedEvent() { var listener = RowAdded; if (listener != null) listener(this, EventArgs.Empty); } 

B code

 private void button1_Click(object sender, EventArgs e) { var frm = new Form2(); frm.RowAdded += new EventHandler<EventArgs>(frm_RowAdded); frm.Show(); } void frm_RowAdded(object sender, EventArgs e) { // retrieve data again } 

You might even consider creating your own EventArgs class, which can contain recently added data. Then you can use this to directly add data to a new row in a DatagridView

+1
source

For datagridview in c # use this code

 con.Open(); MySqlDataAdapter MyDA = new MySqlDataAdapter(); string sqlSelectAll = "SELECT * from dailyprice"; MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, con); DataTable table = new DataTable(); MyDA.Fill(table); BindingSource bSource = new BindingSource(); bSource.DataSource = table; dataGridView1.DataSource = bSource; con.Close(); 

It works to display new records in a datagridview.

+1
source

to update gridview anywhere you only need this code:

 datagridview1.DataSource = "your DataSource"; datagridview1.Refresh(); 
+1
source

my datagridview is editonEnter mode. therefore, it is updated only after I leave the cell or after I move out of the cell again and again.

to trigger this. I do not focus on datagridview. then reorient it.

  this.SelectNextControl(dgv1,true,true,false,true); Application.DoEvents(); //this does magic dgv1.Focus(); 
+1
source

All Articles