Hi, I have two forms, Form1 with a datagridview and Form2 that is meant to add a new record to the linked database in access.

How can i get the datagrid on Form1 to refresh as soon as this is created?
(created once 'Add' button is clicked).

Thanks

Recommended Answers

All 5 Replies

grid.getView().refresh();
grid.getView().refresh();

I can't access it from Form2 which is why I'm having the problem.
I dont know how to do this and any help would be appreciated.

You will have to use delegates.

Here is an example code I did for you:

//form1:
namespace Dec27Delegates
{
    public partial class Form1 : Form
    {
        delegate void DelegateDGV(List<Users> list);
        public Form1()
        {
            InitializeComponent();
            CreatingDGV();
        }

        private void CreatingDGV()
        {
            dataGridView1.Columns.Add("column1", "first name");
            dataGridView1.Columns.Add("column2", "last name");
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = false;

            //adding a row just for info:
            List<Users> list = new List<Users>();
            Users user = new Users();
            user.firstName = "Mitja";
            user.lastName = "Bonca";
            list.Add(user);
            UpdatingDGV(list);
        }

        public void PopulatingDGV(List<Users> list)
        {
            if (dataGridView1.InvokeRequired)
                this.Invoke(new DelegateDGV(UpdatingDGV), new object[] { list });
            else
                UpdatingDGV(list);
        }

        private void UpdatingDGV(List<Users> list)
        {
            foreach(Users user in list)
                dataGridView1.Rows.Add(user.firstName, user.lastName);
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this);
            form2.Show(this);
        }        
    }

    public class Users
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
    }
}

//form2:
public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 _form1)
        {
            InitializeComponent();
            form1 = _form1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string last = textBox1.Text;
            List<Users> list = new List<Users>();
            Users user = new Users();
            user.firstName = name;
            user.lastName = last;
            list.Add(user);
            form1.PopulatingDGV(list);
        }
    }

As you can see I pass data directly from from2 to form1, where I show them in dgv.
What you have to do, is to save (do an insert statement) while on form2, and then you call the same method as mine on form1, which updates dgv. Only your method will get the data from dataBase, and not as mine from the list.

Check this out.
This suits more to your example. I have did the code that you can use it directly, you only have to create methosd for inserting and retreiving data into/from dataBase.
This is the code now:

//form1:
    public partial class Form1 : Form
    {
        delegate void DelegateDGV(DataTable table);
        public Form1()
        {
            InitializeComponent();
            CreatingDGV();
        }

        private void CreatingDGV()
        {
            dataGridView1.Columns.Add("column1", "first name");
            dataGridView1.Columns.Add("column2", "last name");
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = false;
        }

        public void PopulatingDGV()
        {
            DataTable table = GetDataFromDB();
            if (dataGridView1.InvokeRequired)
            {
                this.Invoke(new DelegateDGV(UpdatingDGV), new object[] { table });
            }
        }

        private DataTable GetDataFromDB()
        {
            DataTable table = new DataTable();
            //code for selecting data from db

            return table;
        }

        private void UpdatingDGV(DataTable table) 
        {
            dataGridView1.Rows.Clear();
            foreach (DataRow dr in table.Rows)
            {
                //populate dgv from dr, like:
                int rows = dataGridView1.Rows.Count;
                rows++;
                dataGridView1[0, rows].Value = dr[0].ToString();
                dataGridView1[1, rows].Value = dr[1].ToString();
            }
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(this);
            form2.Show(this);
        }        
    }

    public class Users
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
    }
}

//form2:
    public partial class Form2 : Form
    {
        Form1 form1;
        public Form2(Form1 _form1)
        {
            InitializeComponent();
            form1 = _form1;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = textBox1.Text;
            string last = textBox1.Text;

            // INSTEAD OF PASSING PARAMETER TO FORM1 (LIKE i DO HERE), YOU DO AN INSERT STATEMENT INTO DB.
            //AFTER THAT CALL THE METHOD ON FORM1 (LIKE I DO: form1.PopulatingDGV(); - WITH NO PARAMETERS)       

            InsertDataToDB(name, last);
            form1.PopulatingDGV();
        }

        private void InsertDataToDB(string name, string last)
        {
            //do an insertion into db here
            //use sqlConnection and sqlCommnd
        }
    }

Hope this helps,
Mitja

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.