【问题标题】:Update a DataGridView on Runtime在运行时更新 DataGridView
【发布时间】:2013-04-20 22:48:17
【问题描述】:

我有一个简单的问题。每当我在我的 C# 接口(连接到 SQL 数据库)中插入一些东西时,我都会收到我编程的“已保存数据”的消息(并将数据保存在数据库中),但我的 DataGridView 不会更新,除非我关闭应用程序并再次运行它。

我的代码是:

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);
            }

编辑

我自己找到了答案:就像@Obama 所说的那样。我必须调用 CargarDataGridView(),但在此之前我使用 dataGridView1.Rows.Clear() 删除了所有行;

所以,我的最终代码是:

                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);
            }

【问题讨论】:

  • 你有刷新datagridview的方法吗?如果是这样,我不太懂西班牙语:)
  • 谢谢@Obama,我已经回答了下面的问题。

标签: c# mysql winforms datagridview


【解决方案1】:

就在MessageBox.Show("Datos Guardados", "Mensaje");之后 拨打CargaDataGridView();

【讨论】:

  • 哦,谢谢...我忘了说;如果我这样做,它会更新我的 datagridview,但旧的 datagridview 会保留在那里。例如,如果我在我的数据库中有记录 1 到 4,并且我插入了 5,我的 datagridview 将有记录 1 到 4 和 1 到 5,所以当我想显示 4 时它有 9 条记录
  • 刷新时需要清除datagridview的行(在刷新方法中) CargaDataGridView();通过 dataGridView1.Rows.Clear(); ,如果您认为我帮助您接受了我的回答:)
  • 哈哈哈,我得出了同样的结论:D 但无论如何我都会接受它作为答案,非常感谢,@Obama
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多