Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate a DataGridView on Runtime
    primarykey
    data
    text
    <p>I just have a quick question. Whenever I Insert something into my C# interface (which is connected to a SQL database), I get the message of "Saved Data" that I programmed (and it saves the data in the database), but my DataGridView doesn't updates unless I close the application and run it again. </p> <p>My code is: </p> <pre><code>private void frmCatalogoMunicipios_Load(object sender, EventArgs e) { cmd.Connection = conexion; } private void frmCatalogoMunicipios_Shown(object sender, EventArgs e) { CargaEstados(); CargaDataGridView(); } private void CargaEstados() { conexion.Open(); txtNomMun.Focus(); try { DataSet ds = new DataSet(); MySqlDataAdapter da = new MySqlDataAdapter("SELECT cveestado, nombre FROM tbestados", conexion); da.Fill(ds, "FillDropDown"); cbEstado.DisplayMember = "Nombre"; cbEstado.ValueMember = "CveEstado"; cbEstado.DataSource = ds.Tables["FillDropDown"]; conexion.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void CargaDataGridView() { conexion.Open(); try { cmd.CommandText = "SELECT m.cvemunicipio, m.nombre AS NombreA, e.nombre AS NombreB FROM tbMunicipios m INNER JOIN tbEstados e ON m.CveEstado = e.CVeEstado"; rd = cmd.ExecuteReader(); while (rd.Read()) { this.dataGridView1.Rows.Add(rd.GetValue(0), rd.GetValue(1), rd.GetValue(2)); } conexion.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } conexion.Open(); try { cmd.CommandText = "insert into tbmunicipios (nombre, cveestado) values ('" + txtNomMun.Text + "', '" + cbEstado.SelectedValue.ToString() + "')"; cmd.ExecuteNonQuery(); cmd.Clone(); MessageBox.Show("Datos Guardados", "Mensaje"); conexion.Close(); Nuevo(); } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } </code></pre> <p><strong>EDIT</strong></p> <p>I found the answer myself: like @Obama said. I have to call CargarDataGridView(), BUT before that i deleted all my rows with dataGridView1.Rows.Clear();</p> <p>So, my final code is:</p> <pre><code> conexion.Open(); try { cmd.CommandText = "insert into tbmunicipios (nombre, cveestado) values ('" + txtNomMun.Text + "', '" + cbEstado.SelectedValue.ToString() + "')"; cmd.ExecuteNonQuery(); cmd.Clone(); MessageBox.Show("Datos Guardados", "Mensaje"); conexion.Close(); dataGridView1.Rows.Clear(); CargaDataGridView(); Nuevo(); } catch (Exception ex) { MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload