简体   繁体   中英

Refresh datagridview after update data in another form

I have two forms. Form 1 has a datagridview in side and form 2 has a textbox and a button inside. Value is entered in textbox will be inserted in datagridview after I click the button. I used Show() and ShowDialog() before, but a new form with new data is shown after I clicked button. I also tried Update() and Refresh() but still not working? I need the datagridview refresh itself after I click the button and no new form come out. Anyone can help me? Please....

Here is my code:

Form 1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.dataGridView1.Rows.Add(46.13, 0, 0);
        this.dataGridView1.Rows.Add(105.22, 0, 0);
        this.dataGridView1.Rows.Add(80.67, 0, 0); 
    }


    public void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 newMDIChild = new Form2();
        // Set the parent form of the child window. 
        newMDIChild.MdiParent = this;
        // Display the new form. 
        newMDIChild.Show();

    }

Form 2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

    }


    private void button1_Click(object sender, EventArgs e)
    {
        FormTest.Form1 frm = new Form1();

        frm.dataGridView1.Rows[0].Cells[0].Value = textBox1.Text;

        frm.();

    }
}

Form 2 Button Click use this Code

private void button1_Click(object sender, EventArgs e)
{
   Form frm=(Form)this.MdiParent;
   DataGridView dt = (DataGridView)frm.Controls["dataGridView1"];
   dt.Rows[0].Cells[0].Value = textBox1.Text;

}

You can create a callback interface with one method to add and update data. Implement this in form1 then create a new constructor in form2 with a parameter type of the interface and when creating form2's object pass the reference of the form1 ( this ).

from the form2 when you click on the button just call the interface's method with data to update in gridview as parameter. The interface object here in form2 holds reference of form1 and it will call the function implemented in form1 update the grid.

interface ICallBack { void UpdateGrid(string data); } 

public calss Class1 : ICallBack
{
        // Rest of your code

    void UpdateGrid(string data)
    {
            gridView.Rows[0].Cells[0].Value = data;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Class2 obj = new Class2(this);
        obj.Show();
    }
}

public calss Class2
{
    ICallBack callback;

    public Class2(ICallBack callback)
    {
        this.callback = callback;   
    }

    private void button1_Click(object sender, EventArgs e)
    {
        callback.UpdateGrid(textBox1.Text); 
    }
}

Hope it helps!

If you wish to update/Refresh datagridview on another form. I see two methods: 1. DataTable (Create a datatable and make the source of your datagridview, if you make the changes you will realize the changes are immediately impacting datagridview)

Programmatically to achieve it follow as: Assuming Form-1 has datagridview and Form to has some controls from where you want to add/edit/delete data and want to change Datagridview on Form-1

On Form-2 create a constructor as:

Form myForm1-null;
    public Form-2(Form objForm1)
    {
myForm1=objForm1;
InitializeComponent();
    }

Now you can access myForm1 object like this on any event:

private void btnadd_Click(object sender, EventArgs e)
{
DataGridView dg = (DataGridView)myForm1.Controls["YourDataGridViewName"];
dg.Rows.Add();
}

Now come to Form-1 and open the event on which you are calling Form-2

private void btnShow_Click(object sender, EventArgs e)
{
Form-2 oFrm2 = new Form-2(this);
oFrm2.Show();
}

Hope it helps

Best approach would be using delegates. Use the code as follows.

Assume MainForm has the data grid.

Main Form :

public MainForm()
        {
            InitializeComponent();
        }

public void form2_UpdateEventHandler(object sender, Form2.UpdateEventArgs args)
    {
        dgvItems.DataSource = GetData();
    }

private void MainForm_Load(object sender, EventArgs e)
        {
             dgvItems.DataSource =  GetData();
        }

public DataTable GetData()
        {
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = *your query*
            adp = new SqlDataAdapter(cmd);
            ds.Clear();
            adp.Fill(ds);
            dt = ds.Tables[0];
            con.Close();
            return dt;
        } 
//show Form 2

private void btnSave_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2(this);
            frm.UpdateEventHandler += form2_UpdateEventHandler;
            frm.ShowDialog();

        }

Then assume insert data from Form 2

Form 2 :

//Form 2 constructor  

public Form2(MainForm mainForm)
   {
    InitializeComponent();
   }



 public delegate void UpdateDelegate(object sender, UpdateEventArgs args);
 public event UpdateDelegate UpdateEventHandler;



 public class UpdateEventArgs : EventArgs
    {
      public string Data { get; set; }
    }



protected void InsertData()
    {
       UpdateEventArgs args = new UpdateEventArgs();
       UpdateEventHandler.Invoke(this, args);
    }



 private void btnInsertData_Click(object sender, EventArgs e)
     {
        SqlCommand cmd = new SqlCommand("SP_AddNewRow", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@param1", "Test1");
        cmd.Parameters.AddWithValue("@param2", "Test2");

    if (con.State == ConnectionState.Closed)
       {
         con.Open();
        }
    int n = cmd.ExecuteNonQuery();

    con.Close();

    if (n != 0)
    {
      //your codes if needed
       InsertData();
    }
}

Hope this helps.

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