DataGridView在WinForm中不刷新

问题描述 投票:0回答:1

在过去的几天里,我一直在为如何刷新DataGridView而努力。我终于找到了一些有效的Microsoft文档。它提到我需要删除数据源,重新添加数据源,然后重新填充TableAdapter。

如果我在表单上创建一个按钮并将此代码添加到Click事件,则DataGridView将刷新。

dgvApps.DataSource = null;
dgvApps.DataSource = dStblApplications.tblApplications;
tblApplicationsTableAdapter.Fill(dStblApplications.tblApplications);

但是,如果我将相同的代码以与DataGridView相同的形式放置在Function中,但从另一种形式激活,则将无法使用。我开始调试,发现编译器可以访问并读取代码,但未执行。

//Function that is called to create a popup box asking the user if they would like to re-submit the form.
        public void SaveNewApplicationEntry()
        {
            string message = $"Successfully Created \n Would you like to create another application?";
            string title = "Confirmation Required";
            MessageBoxButtons buttons = MessageBoxButtons.YesNo;
            DialogResult displayMsg = MessageBox.Show(message, title, buttons);
            if (displayMsg == DialogResult.Yes)
            {
                addAppForm.Close();
                frmAddApplication addNewApp = new frmAddApplication();
                addNewApp.Show();
            }
            else
            {
                dgvApps.DataSource = null;
                dgvApps.DataSource = dStblApplications.tblApplications;
                tblApplicationsTableAdapter.Fill(dStblApplications.tblApplications);
                addAppForm.Close();
            }

是否有适当的方法可以在Form2中刷新Form1上的DataGridView?

c# datagridview datasource tableadapter
1个回答
0
投票

您如何从另一种形式调用此函数?请在此处放置代码。

© www.soinside.com 2019 - 2024. All rights reserved.