Hi! I have problem with my payroll program, I'm beginner in c#.
how can I connect or how to Automatic update the datagridview in form1 after I insert a new info from form2? I cant add
the updategrid(); in the form2 because there is no datagridview, only the textboxs.

like when I click the button with ADD command from the form2, the newly inserted info doesn't appear in the form1's datagridview.
I have to click the Refresh button in form1 to appear the new inserted values.

Thanks in advance!

here my codes: "you will see I have commented out the updategrid();"

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Payroll2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        string connStr = "server=localhost;user=root;database=payroll;port=3306;password=''";
        MySqlCommand cmd;
        MySqlDataReader dr;
        //String chk = "";





        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {
                conn.Open();

                string sql = "INSERT INTO empdata (empln, empfn, empmn, nodp, rpd)VALUES ('" + empln.Text + "', '" + empfn.Text + "', '" + empmn.Text + "', '" + nodp.Text + "', '" + rpd.Text + "');";

                cmd = new MySqlCommand(sql, conn);
                cmd.ExecuteNonQuery();

                MessageBox.Show("New Data Inserted");
                //updategrid();
                //refresharea();
            }
            catch (MySqlException e1)
            {
                Console.WriteLine(e1.Message);
            }

            conn.Close();
            this.Close();
        }
    }
}

If the datasource for the datagridview is a datatable or dataset, it should be sufficient to update those with the changes made to the database.
If you made that variable global you can update it from any form.

It's been a while since I used datagridview, but if I recall correctly the datagridview is in constant(?) communication with the datasource and will automatically reflect any changes made.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.