Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to save any change that is made to the Datagridview cell value into the database when the save button is clicked. The code that I have only works when I add a new row. For example: if I have first_name Lina and last_name Pual I want to be able to change only the last name

What I have tried:

<pre>    public EmployeeForm()
    {
        this.bindingSource = new BindingSource();

        this.Load += new EventHandler(EmployeeForm_Load);
    }

    private void EmployeeForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        this.connection.Close();
        this.connection.Dispose();
    }     
    private void RetrieveDataFromTheDatabase()
    {
        
        string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"Employee.mdb\"";
        this.connection = new OleDbConnection(connectionString);

        this.connection.Open();

        OleDbCommand command = connection.CreateCommand();
        command.CommandText = "Select * From Employee";

        this.adapter = new OleDbDataAdapter();
        this.adapter.SelectCommand = command;

        this.dataset = new DataSet();
        this.adapter.Fill(dataset, "Employee");  
        
        OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder(this.adapter);

        commandBuilder.ConflictOption = ConflictOption.OverwriteChanges;

        commandBuilder.GetInsertCommand();
        commandBuilder.GetDeleteCommand();
        commandBuilder.GetUpdateCommand();

    }        

    private void BindControls()
    {
        this.bindingSource.DataSource = this.dataset.Tables[0];
        this.dgvEmployee.DataSource = this.bindingSource;
    }

    void FileSave_Click(object sender, EventArgs e)
    {
        this.adapter.Update(this.dataset, "Employee");
        
        this.dgvEmployee.EndEdit();
        this.bindingSource.EndEdit();
    }
Posted
Comments
Maciej Los 21-Apr-22 13:24pm    
The code you posted is not complete. So, use "Improve question" widget to add missing code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900