Save changes in SQlite after CellEndEdit c #

0

My doubt would be which method I can use for, after modifying a cell of a dataGridView, using the CellEndEdit event, update my database in SQlite.

    
asked by Hector Lopez 26.12.2017 в 11:52
source

1 answer

0

The solution I gave him was;

    public void cargarDataTable()
    {
      dt = new DataTable();

        using (SQLiteConnection conn = new SQLiteConnection(conectionString))
        {
            conn.Open();
            using (SQLiteCommand cmd = new SQLiteCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = "select * from clientes;";
                SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
                da.Fill(dt);
            }
            conn.Close();

        }
    }
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        try { 
        String id = dataGridView1.CurrentRow.Cells["n_historia"].Value.ToString();
        string col = dt.Columns[e.ColumnIndex].ColumnName;
        string data = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value + "";

        string sql = string.Format("UPDATE clientes SET '{0}' = '{1}' WHERE n_historia = {2};", col, data, id);

        using (SQLiteConnection conn = new SQLiteConnection(conectionString))
        {
            conn.Open();
            using (SQLiteCommand cmd = new SQLiteCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery();
            }
            conn.Close();
        }
        }
        catch { MessageBox.Show("No es posible cambiar el numero de historia del cliente."); }

    }

With this code an update is obtained in the BD of the field affected by the CellEndEdit event.

    
answered by 11.01.2018 / 11:43
source