简体   繁体   English

在我的数据库中插入和更新我的值后,如何从另一个表单刷新我的数据网格视图?

[英]How do I refresh my datagrid view from another form after I insert and update my values inside my database?

I am not sure how to proceed with refreshing the data grid view after I update and insert values into my database.在更新并将值插入数据库后,我不确定如何继续刷新数据网格视图。 I know that I have to bind the database values to the other form so that they will fill my DGV in the other form with the new content when I click the update or insert button.我知道我必须将数据库值绑定到另一个表单,以便当我单击更新或插入按钮时,它们将在另一个表单中用新内容填充我的 DGV。

so far this is what I have到目前为止,这就是我所拥有的

this is my code for populating the Datagrid:这是我填充数据网格的代码:

private void PopulateDataGrid()
    {

        Form1 f1 = new Form1();

        MySqlCommand cmd = conn.CreateCommand();
        DataTable datatable = new DataTable();

        cmd.CommandText = "select id,platenumber,brand,model,yearmodel,regdate,exdate,odometer from vehicle";
        cmd.CommandType = CommandType.Text;

        dataAdapter = new MySqlDataAdapter(cmd);
        dataAdapter.Fill(datatable);
        f1.dataGridView1.DataSource = datatable;

    }

but when I call the function in my save button it seems to have skipped it.但是当我在我的保存按钮中调用 function 时,它似乎已经跳过了它。

private void savebtn_Click(object sender, EventArgs e)
        {

            Form1 f1 = new Form1();
            DataTable dt = new DataTable();

           int newid = Convert.ToInt32(idtxt.Text);

           int ID = newid;

            MySqlCommand cmd = new MySqlCommand("update vehicle set platenumber=@platenumber, brand=@brand, model=@model, yearmodel=@yearmodel, regdate=@regdate, exdate=@exdate, odometer=@odometer where ID = @id" , conn);

            cmd.Parameters.AddWithValue("@id", ID);
            cmd.Parameters.Add("@platenumber", MySqlDbType.VarChar, 10).Value = pnumber.Text;
            cmd.Parameters.Add("@brand", MySqlDbType.VarChar, 60).Value = brand.Text;
            cmd.Parameters.Add("@model", MySqlDbType.VarChar, 45).Value = model.Text;
            cmd.Parameters.Add("@yearmodel", MySqlDbType.Int32).Value = yearmodel.Text;
            cmd.Parameters.Add("@regdate", MySqlDbType.Date).Value = datereg.Value;
            cmd.Parameters.Add("@exdate", MySqlDbType.Date).Value = regexp.Value;
            cmd.Parameters.Add("@odometer", MySqlDbType.Decimal).Value = odometer.Text;

            int i = cmd.ExecuteNonQuery();
            if (i != 0)
            {
                MessageBox.Show("Success");
            }
            else
            {   
                MessageBox.Show("Fail");
            }


            PopulateDataGrid();

            

            this.Close();
        }

I am confused about what I should do to refresh the DGV once I click a button to update/ insert in my database and close the separate form.一旦单击按钮更新/插入我的数据库并关闭单独的表单,我对刷新 DGV 应该做什么感到困惑。

Slightly confused about what you are asking for but refresh();对您要的内容感到有些困惑,但是 refresh(); should update your datagridview应该更新你的datagridview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以另一种形式将我的数据网格视图项显示到文本框? - How do I display my datagrid view items to textboxes in a another form? 如何使用数据库中的值填充CheckBoxList? - How do I populate my CheckBoxList with values from my database? 如何用最少的代码将许多文本框中的值插入数据库? - How do I insert values from many textboxes into my database with minimal code? 如何更新我的数据库? - How do I update my database? 如何使用 ViewModel 中的数据表填充视图中的数据网格 - How do I fill a datagrid in my View with my datatable in my ViewModel 如何从我的角度调用另一个 controller - How do I call another controller from my view 更新数据库源后,如何让 Visual Studio 2010 刷新我的数据集? - How do I get Visual Studio 2010 to refresh my DataSet after I have updated the Database source? 每当计时事件发生时,如何在表单上的ListView中刷新数据? - How do I Refresh Data inside the ListView on my form everytime the time ticker event occurs C# 如何在不使用命令生成器的情况下将 DATASET 中的行插入到我的 DATABASE 中? - How do I insert Rows from my DATASET to my DATABASE without using command builder? 如何从我的控制器更新数据库以获取ASP.NET中已列出的值? - How can I update my database from my controller for values already listed in ASP.NET?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM