【问题标题】:Can't update DataGridView from local database无法从本地数据库更新 DataGridView
【发布时间】:2020-04-26 17:01:23
【问题描述】:

我有:

  • 1 个表格
    • 3 个面板(1 个左侧用于菜单/1 个顶部和 1 个填充了我页面的其他部分。
      • 几个控制用户(与菜单中按钮的数量一样多)

在 1 个控制用户中,我有 2 个 DataGridView。 2 Datagrid in the Control User

当我点击其中一行时,会打开另一个表单。

enter image description here

我从本地主机数据库中获取数据。当我加载程序时,没问题,一切都更新了。

当我在打开的表单上单击“Validator cette paie”时,它应该将此行插入另一个表并删除表的信息。加上刷新两个数据网格(所以我从第一个数据网格中删除的行应该出现在第二个数据网格中。并关闭表单。

插入和删除工作完美,也关闭了我的表单。但不刷新。 如果你注意到我有 2 个刷新按钮,当我点击它们时它会刷新,但我希望它是自动的。(否则会让用户感到困惑)

public void BtnValidationPaie_Click(object sender, EventArgs e)
    {
        try
        {
            string InsertPaie = "INSERT INTO paievalide(Prenom,AmiLocal,Extension,SBN,CheckIn,PaieHT,PaieTTC) VALUES('" + LblPrenom1.Text + "','" + LblAmiLocal1.Text + "', '" + LblExtension1.Text + "', '" + LblSBN1.Text + "', '" + LblCheckIn1.Text + "', '" + LblPaieHT1.Text + "', '" + LblPaieTTC1.Text + "')";
            MySqlFunctionEmploye.ExecuteQuery(InsertPaie);

            string deleteQuery = "DELETE FROM newpaie WHERE ID =" + LblInfo1.Text;
            MySqlFunctionEmploye.ExecuteQuery(deleteQuery);


            AlerteControl AlerteControl = new AlerteControl();
            AlerteControl.MajDatagrids(); // **Calling the method to update DATAGRIDVIEWS**   

            //FunctionAlerte FunctionAlerte = new FunctionAlerte();
            //FunctionAlerte.RefreshDataGridViewNewPaie(dataGridViewNewAlerte); **This is actually the function i call in my method above. If i call the function right away i got an error saying my datagrid is Null, which can't be null all the cells are filled up.**
            //FunctionAlerte.RefreshDataGridViewPaieValide(dataGridViewPaieValide);
            this.Close();

            Console.WriteLine("pas d'erreur");
        }catch(MySqlException exception)
        {
            MessageBox.Show(exception.ToString());
        }
    }

这是我调用函数的方法。

public void MajDatagrids()
    {
        Console.WriteLine("Maj de MajDATAgrids"); **I checked if the method is called by this line and it is called.**
        FunctionAlerte.RefreshDataGridViewNewPaie(dataGridViewNewAlerte);
        FunctionAlerte.RefreshDataGridViewPaieValide(dataGridViewPaieValide);
    }

我已经找了很久了,但我什么也找不到。还请理解我对 c# 和 Visual Studio ( Winform ) 很陌生

编辑:这是第二种形式的代码

public partial class InfosNewAlerte : Form
{
    public InfosNewAlerte()
    {
        InitializeComponent();

    }
    MySqlConnection connection = new MySqlConnection("Server=localhost; database=cap; user id=root; pwd=");
    public DataGridView dataGridViewNewAlerte;
    public DataGridView dataGridViewPaieValide;

    //
    // Au Chargement de la page, on recupere les infos de la DATAGRID-NEW-ALERTE et on les affiches sur le nouveau formulaire INFOS NEWS ALERTE FORMULAIRE
    //
    public void InfosNewAlerte_Load(object sender, EventArgs e)
    {
        LblInfo1.Text = AlerteControl.id;
        LblPrenom1.Text = AlerteControl.prenom;
        LblAmiLocal1.Text = AlerteControl.amilocal;
        LblSBN1.Text = AlerteControl.sbn;
        LblExtension1.Text = AlerteControl.extension;
        LblCheckIn1.Text = AlerteControl.checkin;
        LblPaieHT1.Text = AlerteControl.paieht;
        LblPaieTTC1.Text = AlerteControl.paiettc;
    }

    //
    // En cliquant sur le bouton valider de cette page alors vous envoyez cette paie a la partie finance et la SUPPRIME de New Alerte
    //

    public void BtnValidationPaie_Click(object sender, EventArgs e)
    {
        try
        {
            string InsertPaie = "INSERT INTO paievalide(Prenom,AmiLocal,Extension,SBN,CheckIn,PaieHT,PaieTTC) VALUES('" + LblPrenom1.Text + "','" + LblAmiLocal1.Text + "', '" + LblExtension1.Text + "', '" + LblSBN1.Text + "', '" + LblCheckIn1.Text + "', '" + LblPaieHT1.Text + "', '" + LblPaieTTC1.Text + "')";
            MySqlFunctionEmploye.ExecuteQuery(InsertPaie);

            string deleteQuery = "DELETE FROM newpaie WHERE ID =" + LblInfo1.Text;
            MySqlFunctionEmploye.ExecuteQuery(deleteQuery);


            AlerteControl AlerteControl = new AlerteControl();
          //  AlerteControl.MajDatagrids(); // Appel de la methode de MISE A JOUR DES DATAGRIDVIEWS   


            this.Close();
            FunctionAlerte FunctionAlerte = new FunctionAlerte();
            FunctionAlerte.RefreshDataGridViewNewPaie(dataGridViewNewAlerte);
            FunctionAlerte.RefreshDataGridViewPaieValide(dataGridViewPaieValide);
            Console.WriteLine("pas d'erreur");


        }
        catch(MySqlException exception)
        {
            MessageBox.Show(exception.ToString());
        }
    }
}

【问题讨论】:

  • 你可以创建一个刷新DatagridView的方法。所以在插入和更新之后,您可以调用该方法来刷新datagridview。此外,您也可以在刷新按钮中使用此方法。
  • 对对对,我就是这么做的。调用方法majDatagrid();

标签: c# datagridview


【解决方案1】:

您只需要在BtnValidationPaie_Click

末尾加载GridView中的数据时调用您的方法
   private readonly FunctionAlerte  frm1; //readonly is optional (For safety purposes)

    public partial class InfosNewAlerte : Form
{
    public InfosNewAlerte()
    {
        InitializeComponent();

    }
    MySqlConnection connection = new MySqlConnection("Server=localhost; database=cap; user id=root; pwd=");
    public DataGridView dataGridViewNewAlerte;
    public DataGridView dataGridViewPaieValide;

    //
    // Au Chargement de la page, on recupere les infos de la DATAGRID-NEW-ALERTE et on les affiches sur le nouveau formulaire INFOS NEWS ALERTE FORMULAIRE
    //
    public void InfosNewAlerte_Load(object sender, EventArgs e)
    {
        LblInfo1.Text = AlerteControl.id;
        LblPrenom1.Text = AlerteControl.prenom;
        LblAmiLocal1.Text = AlerteControl.amilocal;
        LblSBN1.Text = AlerteControl.sbn;
        LblExtension1.Text = AlerteControl.extension;
        LblCheckIn1.Text = AlerteControl.checkin;
        LblPaieHT1.Text = AlerteControl.paieht;
        LblPaieTTC1.Text = AlerteControl.paiettc;
    }

    //
    // En cliquant sur le bouton valider de cette page alors vous envoyez cette paie a la partie finance et la SUPPRIME de New Alerte
    //

    public void BtnValidationPaie_Click(object sender, EventArgs e)
    {
        try
        {
            string InsertPaie = "INSERT INTO paievalide(Prenom,AmiLocal,Extension,SBN,CheckIn,PaieHT,PaieTTC) VALUES('" + LblPrenom1.Text + "','" + LblAmiLocal1.Text + "', '" + LblExtension1.Text + "', '" + LblSBN1.Text + "', '" + LblCheckIn1.Text + "', '" + LblPaieHT1.Text + "', '" + LblPaieTTC1.Text + "')";
            MySqlFunctionEmploye.ExecuteQuery(InsertPaie);

            string deleteQuery = "DELETE FROM newpaie WHERE ID =" + LblInfo1.Text;
            MySqlFunctionEmploye.ExecuteQuery(deleteQuery);


            AlerteControl AlerteControl = new AlerteControl();
          //  AlerteControl.MajDatagrids(); // Appel de la methode de MISE A JOUR DES DATAGRIDVIEWS   




            frm1.RefreshDataGridViewNewPaie(dataGridViewNewAlerte);
            frm1.RefreshDataGridViewPaieValide(dataGridViewPaieValide);
            Console.WriteLine("pas d'erreur");

           this.Close();

        }
        catch(MySqlException exception)
        {
            MessageBox.Show(exception.ToString());
        }
    }
}

【讨论】:

  • 所以我移动了那些你提到的2行,仍然没有。 AlerteControl AlerteControl = new AlerteControl();<br/> AlerteControl.MajDatagrids(); // **Calling the method to update DATAGRIDVIEWS**
  • 您可以将您的绑定代码绑定到GridView span>
  • 我有一个类,其中有2个函数刷新dataGrid。(在评论下添加了代码) span>
  • 我更新了我的答案。请单击检查是否回答您的问题 span>
  • 现在它表示“refreshdatagridviewnewpaie”和“refreshdatagridviewpaievalide”的上下文不存在。这就是为什么在我尝试同样的方式与FunctionAlerte.RefreshDataGridViewNewPaie(dataGridViewNewAlerte);但是给我一个空次引用的错误 span>
猜你喜欢
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 2015-05-20
  • 1970-01-01
相关资源
最近更新 更多