【发布时间】:2020-02-06 15:54:05
【问题描述】:
过去几天我一直在琢磨如何刷新 DataGridView。我终于找到了一些有用的微软文档;它提到我需要删除 DataSource,重新添加 DataSource,并重新填充 TableAdapter。
如果我在表单上创建一个按钮并将此代码添加到 Click 事件中,DataGridView 将刷新。
dgvApps.DataSource = null;
dgvApps.DataSource = dStblApplications.tblApplications;
tblApplicationsTableAdapter.Fill(dStblApplications.tblApplications);
但是,如果我将相同的代码放在与 DataGridView 相同的表单中的 Function 中,但从另一个表单激活,它将不起作用。我开始调试,发现代码被编译器到达,并且被读取,但是没有被执行。
//This is on Form1. 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();
}
//This is on Form2
private void btnSubmit_Click(object sender, EventArgs e)
{
newApp.AppName = txtAppName.Text;
newApp.AppDescription = txtAppDescription.Text;
newApp.AppMasterLocation = txtAppMasterLoc.Text;
newApp.AppCurrentVersion = txtAppCurrentVer.Text;
newApp.ActiveDate = dtpAppActiveDate.Value == null ? DateTime.Now : dtpAppActiveDate.Value;
newApp.InactiveDate = dtpAppInactiveDate.Value == null ? DateTime.Now : dtpAppInactiveDate.Value;
if (!newApp.AppName.Equals("") && !newApp.AppDescription.Equals("") && !newApp.AppMasterLocation.Equals("") && !newApp.AppCurrentVersion.Equals(""))
{
frmApplications mainApplicationForm = new frmApplications();
mainApplicationForm.SaveNewApplicationEntry();
}
}
是否有适当的方法从 Form2 中刷新 Form1 上的 DataGridView?
【问题讨论】:
标签: c# datagridview datasource tableadapter