[c#] C# refresh DataGridView when updating or inserted on another form

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

form A allowes user to insert and update student information.

form b is only a DataGridView and button there.

When I insert student on form A, then I go to form B, the new student did not show on the DataGridView , and if I rerun the program, the new student will appear in form B.

I tried using this on button on form b

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

but it's still not working.


Edited:

My code for inserting 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) doesn't automatically update when I inserted a new worker. But if I rerun the application, the new worker appears.

This question is related to c# datagridview

The answer is


for refresh data gridview in any where you just need this code:

datagridview1.DataSource = "your DataSource";
datagridview1.Refresh();

Create a small function and use it anywhere

public SqlConnection con = "Your connection string"; 
public void gridviewUpdate()
{
    con.Open();
    string select = "SELECT * from table_name";
    SqlDataAdapter da = new SqlDataAdapter(select, con);
    DataSet ds = new DataSet();
    da.Fill(ds, "table_name");
    datagridview.DataSource = ds;
    datagridview.DataMember = "table_name";
    con.Close();
}

DataGridView.Refresh and And DataGridView.Update are methods that are inherited from Control. They have to do with redrawing the control which is why new rows don't appear.

My guess is the data retrieval is on the Form_Load. If you want your Button on Form B to retrieve the latest data from the database then that's what you have to do whatever Form_Load is doing.

A nice way to do that is to separate your data retrieval calls into a separate function and call it from both the From Load and Button Click events.


my datagridview is editonEnter mode . so it refresh only after i leave cell or after i revisit and exit cell twice.

to trigger this iimedately . i unfocus from datagridview . then refocus it.

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

putting a quick example, should be a sufficient starting point

Code in Form A

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);
}

Code in Form B

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 can even consider creating your own EventArgs class that can contain the newly added data. You can then use this to directly add the data to a new row in DatagridView


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 for show new records in the datagridview.