eusataf
eusataf

Reputation: 897

How to update changes in the data source after changing the cell value in the dataGridView?

The user makes changes to the cell.
The CellEndEdit event is triggered.
In the Save () method, changes are displayed in the adapter.Update (table_2); line.
Changes are not displayed in the database (changes are not saved to the database).
 
If the user calls the Save () method using the button, the changes are displayed in the database (in the data source table dataGridView).
 
Question.
How to update changes in the data source after changing the cell value in the dataGridView?

My Code

 private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            i++;
            Save();
            richTextBox1.Text += i + " Event - `CellEndEdit` \r\n";
        }


     public DataTable CreateCmds()
            {
                table_2 = new DataTable();
                try
                {
                    string connectionString = @"Data Source=.\SQLEXPRESS1;Initial Catalog=Prb;Integrated Security=True";              

                    string queryString = "SELECT * FROM tbl_01_Groups";

                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        adapter = new SqlDataAdapter();
                        adapter.SelectCommand = new SqlCommand(queryString, connection);
                        SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

                        connection.Open();

                        adapter.Fill(table_2);
                    }

                }
                catch (Exception ex)
                {

                    string s = ex.Message;
                    string t = ex.StackTrace;
                    // throw;
                }

                return table_2;
            }

            public void Save()
            {
                string connectionString = @"Data Source=.\SQLEXPRESS1;Initial Catalog=Prb;Integrated Security=True";

                string queryString = "SELECT * FROM tbl_01_Groups";

                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand(queryString, connection);
                    SqlCommandBuilder builder = new SqlCommandBuilder(adapter);

                    connection.Open();

                    adapter.Update(table_2);
                }
            }

enter image description here

Update_1
The same thing happens when I use the CellValueChanged event.
The CellValueChanged event is triggered.
In the Save () method, changes are displayed in the adapter.Update (table_2); line.
Changes are not displayed in the database (changes are not saved to the database).

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            i++;
            Save();
            richTextBox1.Text += i + " Event - `CellValueChanged` \r\n";
        }

Upvotes: 1

Views: 1340

Answers (1)

Davide Vitali
Davide Vitali

Reputation: 1035

MSDN documentation on the CellEndEdit:

Occurs when edit mode stops for the currently selected cell.

and follows an example on how to catch this event triggering in case of misvalidation - that's why it stops: not because the user has finished, but because a validation event occurred. On the other side, MSDN documentation on the CellValueChanged says that

The DataGridView.CellValueChanged event occurs when the user-specified value is committed, which typically occurs when focus leaves the cell.

which is your case. Cheers!

Upvotes: 1

Related Questions