Hi,

I currently have a dataGridView which is populated with data. However, I now need to be able to add or edit the data in that dataGridView and the changes then update my SQL database.

My code for the dataGridView

private void main_Load(object sender, EventArgs e)
        {
            
            //create the connection string
            string connString = @"Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False";

            //create the database query
            string query = "SELECT * FROM coffeeType";

            //create an OleDbDataAdapter to execute the query
            SqlDataAdapter dAdapter = new SqlDataAdapter(query, connString);

            //create a command builder (SQL)
            SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);

            //create a DataTable to hold the query results
            DataTable dTable = new DataTable();

            //fill the DataTable
            dAdapter.Fill(dTable);            
            
            //No need to create DataGridView as exists in designer.
            //DataGridView dgView = new DataGridView();

            //BindingSource to sync DataTable and DataGridView
            BindingSource bSource = new BindingSource();

            //set the BindingSource DataSource
            bSource.DataSource = dTable;

            //set the DataGridView DataSource
            dataGridView1.DataSource = bSource;
                     
        }

As said, my question is how can I now add a row for example and click a SAVE button so the changes update my SQL database.

Thanks

>click a SAVE button so the changes update my SQL database.

dAdapter.Update(dTable);
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.