bandaa
bandaa

Reputation: 169

how to update data grid view in winforms?

I have a page in my winforms, where i want to display all the data on a table so the user can see the information in the table but on the winforms. i have not done this before so need some guidance from you people, i want it to consistently (when new records are added) update and be editable so a row can be deleted if it needs to. the table i have is "booking" and the form on which i can add data to this table is "frmBooking" so i want booking data made whilst the program is run to be stored in the datagridview so the user can view their bookings made. thanks the code i have is:

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);

        }
    }
}

Upvotes: 0

Views: 2940

Answers (1)

CathalMF
CathalMF

Reputation: 10055

You can use the DataSource property of the datagridview which binds the datagridview to a source. eg. A DataTable.

Whenever the datatable is edited the datagridview is updated.

You could also modifiy the datagridview manually by adding rows and columns but this is a bit more work.

Edit: If you wish your changes to update to the datagridview you need to modify the datatable which is the binding source.

Upvotes: 3

Related Questions