C# 更新或插入另一个表单时刷新 DataGridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15526067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:03:51  来源:igfitidea点击:

C# refresh DataGridView when updating or inserted on another form

c#datagridview

提问by Tham JunKai

I have 2 forms which are form Aand form B,

我有两种形式,分别是form Aform B

form Aallowes user to insert and update student information.

form A允许用户插入和更新学生信息。

form bis only a DataGridView and button there.

form b只是一个 DataGridView 和按钮。

When I insert student on form A, then I go to form B, the new student did not show on the DataGridView , and if I rerun the program, the new student will appear in form B.

当我插入学生时form A,然后我转到form B,新学生没有显示在 DataGridView 上,如果我重新运行程序,新学生将出现在form B.

I tried using this on button on form b

我尝试在表单 b 上的按钮上使用它

datagridview1.refresh();
datagridview1.update();

but it's still not working.

但它仍然无法正常工作。



Edited:

编辑:

My code for inserting a worker

我的插入工人的代码

cmd = new OleDbCommand("insert into FWINFOS (ID,Name,Gender,DateOfBirth,Race,WorkingPlace,PassportNO,DateOfExpire,[Position],Photo) values('" + textBox5.Text + "','" + textBox1.Text + "','" + textBox2.Text + "','" + dateTimePicker1.Value + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + dateTimePicker2.Value + "',@Position,@Photo)", con);


        cmd.Parameters.AddWithValue("@Position", comboBox1.SelectedText.ToString());
        conv_photo();

        con.Open();
        int n = cmd.ExecuteNonQuery();
        //cmd.ExecuteNonQuery();
        con.Close();
        if (n > 0)
        {
            MessageBox.Show("Inserted");
            loaddata();

            rno++;
        }
        else
            MessageBox.Show("No Insert");



    }

my Datagridview1(Form2)doesn't automatically update when I inserted a new worker. But if I rerun the application, the new worker appears.

Datagridview1(Form2)当我插入一个新的工作不会自动更新。但是如果我重新运行应用程序,新的工作人员就会出现。

采纳答案by apomene

// Form A
public void loaddata()
{
    //do what you do in load data in order to update data in datagrid
}

then on Form B define:

然后在表格 B 上定义:

// Form B
FormA obj = (FormA)Application.OpenForms["FormA"];

private void button1_Click(object sender, EventArgs e)
{
    obj.loaddata();
    datagridview1.Update();
    datagridview1.Refresh();
}

回答by Conrad Frix

DataGridView.Refreshand And DataGridView.Updateare methods that are inherited from Control. They have to do with redrawing the control which is why new rows don't appear.

DataGridView.RefreshDataGridView.Update是从 Control 继承的方法。它们与重绘控件有关,这就是不出现新行的原因。

My guess is the data retrieval is on the Form_Load. If you want your Button on Form B to retrieve the latest data from the database then that's what you have to do whatever Form_Load is doing.

我的猜测是数据检索在 Form_Load 上。如果您希望 Form B 上的 Button 从数据库中检索最新数据,那么无论 Form_Load 正在做什么,您都必须这样做。

A nice way to do that is to separate your data retrieval calls into a separate function and call it from both the From Load and Button Click events.

一个很好的方法是将您的数据检索调用分离到一个单独的函数中,并从 From Load 和 Button Click 事件中调用它。

回答by NoviceProgrammer

putting a quick example, should be a sufficient starting point

举一个简单的例子,应该是一个足够的起点

Code in Form A

表格 A 中的代码

public event EventHandler<EventArgs> RowAdded;

private void btnRowAdded_Click(object sender, EventArgs e)
{
     // insert data
     // if successful raise event
     OnRowAddedEvent();
}

private void OnRowAddedEvent()
{
     var listener = RowAdded;
     if (listener != null)
         listener(this, EventArgs.Empty);
}

Code in Form B

表格 B 中的代码

private void button1_Click(object sender, EventArgs e)
{
     var frm = new Form2();
     frm.RowAdded += new EventHandler<EventArgs>(frm_RowAdded);
     frm.Show();
}

void frm_RowAdded(object sender, EventArgs e)
{
     // retrieve data again
}

You can even consider creating your own EventArgsclass that can contain the newly added data. You can then use this to directly add the data to a new row in DatagridView

您甚至可以考虑创建自己的EventArgs类来包含新添加的数据。然后,您可以使用它直接将数据添加到 DatagridView 中的新行

回答by Shkar

For datagridview in C#, use this code

对于 C# 中的 datagridview,使用此代码

con.Open();
MySqlDataAdapter MyDA = new MySqlDataAdapter();
string sqlSelectAll = "SELECT * from dailyprice";
MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, con);

DataTable table = new DataTable();
MyDA.Fill(table);

BindingSource bSource = new BindingSource();
bSource.DataSource = table;


dataGridView1.DataSource = bSource;
con.Close();

It works for show new records in the datagridview.

它适用于在 datagridview 中显示新记录。

回答by mohamad kiani

for refresh data gridview in any where you just need this code:

在您只需要此代码的任何地方刷新数据网格视图:

datagridview1.DataSource = "your DataSource";
datagridview1.Refresh();

回答by bh_earth0

my datagridview is editonEnter mode . so it refresh only after i leave cell or after i revisit and exit cell twice.

我的 datagridview 是 editonEnter 模式。所以它只有在我离开单元格或我重新访问并退出单元格两次后才会刷新。

to trigger this iimedately . i unfocus from datagridview . then refocus it.

立即触发。我不关注 datagridview 。然后重新聚焦。

 this.SelectNextControl(dgv1,true,true,false,true);    
 Application.DoEvents();    //this does magic
 dgv1.Focus();