简体   繁体   中英

How to refresh Data Grid view from other form in c# without reopening?

I have a main MDI form with two forms inside. One is EmployeeAdd and other is EmployeeView. There is a button on EmployeeAdd to save inserted data in database. and in EmployeeView, there is a Data Grid to view data from database. the problem is i want to update data grid imediately without reopening the main form. here is code of my save button of EmployeeAdd form.

        try
       {
        con.ConnectionString = constr;
        con.Open();
        OleDbCommand comnd = new OleDbCommand();
        comnd.Connection = con;
        comnd.CommandText = "INSERT INTO employees([EmpName],[EmpF_Name],[CNIC],[DOB],MobileNo,OtherNo,DOJ,EmpAddress,RefName,RefMobile,RefOtherNo,RefAddress) VALUES('"
        + txtEmpName.Text + "','" + txtFName.Text + "','" + mTxtCNIC.Text + "','" + mTxtDOB.Text
        + "','" + mTxtMobile.Text + "','" + txtOther.Text + "','" + dateTimePicker1.Text + "','" + txtAddress.Text
        + "','" + txtRefName.Text + "','" + mTxtRefMobile.Text + "','" + txtRefOther.Text + "','" + txtRefAddress.Text + "')";
        OleDbDataReader reader = comnd.ExecuteReader();
        MessageBox.Show("Record has been saved successfully", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);}            catch (Exception ex)
      {
        MessageBox.Show(ex.Message);
      }
        con.Close(); 

基本上,您需要编写一种从DB加载所有记录的方法,然后在每次添加员工时清除数据网格视图并重新加载。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM