0
votes

i have the following code, what want it to do is update when i add new records to my table from a separate form, at the moment i am getting all the table data, but if i add new data it is not visible here, but is saved in the table, i have to close the program and run it again for the saved data to be visible in the datagridview. my question is how do i update the datagridview, in order for the table info to be there at all times. The code i have is as follows:

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;

namespace project
{
    public partial class frmViewBookings : Form
    {
        public frmViewBookings()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Form3 mainpage = new Form3();
            mainpage.Show();
            this.Close();
        }

        private void frmViewBookings_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed.
            this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);

        }
    }
}
1
Are you calling .DataBind() after updating? - DGibbs
What does the Binding Look Like? can you show where you are a declaring usersDataSet1 looks like you are missing some code here - MethodMan
hmm... i think i am confused, @DJKRAZE - i had simply added the datagridview from the toolbox and then simply chose the data source. this is all i have done - bandaa
sounds like you are not Binding then just like I stated initially - MethodMan
how do i bind the data - bandaa

1 Answers

0
votes

Add/Edit your existing method in frmViewBookings:

private void frmViewBookings_Load(object sender, EventArgs e)
{
    // TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed.
    UpdateData();

}

public void UpdateData()
{
    this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);
}

Then call UpdateData() from your other Form

In your other form, add a property that holds the first form:

public partial class frmBooking : Form
{
    public frmViewBookings myBookingsFrm = null;
}

Then when you create the frmBooking form, do:

frmBooking frmNewBookingFrm = new frmBooking();
frmNewBookingFrm.myBookingsFrm = this;//or whatever reference to the first frmViewBookings is

So after you insert the new record inside frmBooking, call:

myBookingsFrm.UpdateData();