简体   繁体   English

仅在关闭和打开对话框后更新数据库

[英]Database only updates after closing and opening dialog

I have a DataGridView inside a form that displays data from a database every time I load the page like this: 我在窗体内有一个DataGridView,每次加载该页面时,该窗体都会显示来自数据库的数据:

private void LoadList(string Input)
{
    fieldsDataGridView.DataSource = null;
    List<Field> fields = new List<Field>();
    fields = fieldsData.GetAllByTaskId(Input);
    List<FieldsDGViewModel> fdgvm = new List<FieldsDGViewModel>();
    foreach (var item in fields)
    {
        var f = new FieldsDGViewModel
        {
            Id = item.Id,
            Name = item.Name,
            Order = item.Order,
            IsPrint = item.IsPrint
        };
        fdgvm.Add(f);
    }
    fdgvm = fdgvm.OrderBy(x => x.Order).ToList();
    fieldsDataGridView.DataSource = fdgvm;
    fieldsDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    fieldsDataGridView.Columns["Id"].Visible = false;
    fieldsDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}

When I double click on an entry in my list, it opens a dialog box containing a form and loads the respective details from that entry. 当我双击列表中的一个条目时,它将打开一个包含表单的对话框,并从该条目中加载相应的详细信息。 When I save the details, the dialog box closes and in the class where my DataGridView sits, there is this FormClose function that refreshes the DataGridView. 当我保存详细信息时,对话框将关闭,并且在DataGridView所在的类中,此FormClose函数将刷新DataGridView。

private void FormClosed(object sender, FormClosedEventArgs e)
{
    RefreshDataGrid();
    RecursiveClearInputs(this.Controls);
    fieldIdInput.Text = "";
}

private void RefreshDataGrid()
{
    var selected = programInput.SelectedValue;
    if (selected != null)
    {
        var result = programsData.Get(selected.ToString());
        if (result != null)
        {
            programIdInput.Text = result.Id;
            LoadList(result.Id);
        }
    }
    if (selected == "-1")
    {
        RecursiveClearInputs(this.Controls);
        programIdInput.Text = "";
        fieldIdInput.Text = "";
        fieldsDataGridView.DataSource = null;
    }
    fieldsDataGridView.ClearSelection();
}

However, I am having this issue where the only way that my DataGridView refreshes properly is if I close the main form I am on and reopen it again. 但是,我遇到的问题是,正确刷新DataGridView的唯一方法是,如果我关闭自己所在的主窗体,然后再次将其重新打开。

I debugged and managed to capture some results. 我调试并设法捕获了一些结果。

Image1: Directly after the Update form is closed. Image1:直接在更新表单关闭后。 In the fields list, only one entry can have IsPrint = true. 在字段列表中,只有一个条目可以具有IsPrint = true。 However the image shows that both are true. 但是,图像显示两者都是正确的。

在此处输入图片说明

Image2: After I close and reopen the page containing the DataGridView, it shows this correct result. Image2:关闭并重新打开包含DataGridView的页面后,它将显示此正确结果。 Only 1 IsPrint = true. 只有1 IsPrint = true。

在此处输入图片说明

I have tried many methods to solve this issue but I'm not sure why its not refreshing properly. 我已经尝试了许多方法来解决此问题,但是我不确定为什么它无法正确刷新。

This is how I open a dialog 这就是我打开对话框的方式

EditFields editFields = new EditFields(programIdInput.Text, fieldIdInput.Text, false);
editFields.FormClosed += new FormClosedEventHandler(FormClosed);
editFields.ShowDialog();

EDIT: 编辑:

I added a dialogresult check but it still doesn't update the datagridview properly. 我添加了一个dialogresult检查,但是它仍然无法正确更新datagridview。 Maybe its a thread issue? 也许是线程问题?

dr = editFields.ShowDialog();
if (dr == DialogResult.OK)
{
    RefreshDataGrid();
    RecursiveClearInputs(this.Controls);
    fieldIdInput.Text = "";
}

I'm just assuming how the OnCklick event looks where you open your Dialog, but I think the problem is that you try to refresh the Datagrid form another Form / Thread. 我只是假设OnCklick事件在打开Dialog时的外观,但我认为问题在于您试图通过另一个Form / Thread刷新Datagrid。 (which will not work) (这将不起作用)

I suggest you open the dialog form by using ShowDialog and refresh the grid after the Form was closed in the onclick event itself. 我建议您使用ShowDialog打开对话框窗体,并在onclick事件本身中关闭窗体后刷新网格。 Try replacing 尝试更换

RefreshDataGrid();

in your FormClosed event with 在您的FormClosed事件中

DialogResult = DialogResult.OK;

Then you are able to handle the grid reloading in your onclick event like this: 然后,您可以像这样在onclick事件中处理网格重新加载:

 EditFields editFields = new EditFields(programIdInput.Text, fieldIdInput.Text, false);

   if (editFields.ShowDialog(this) == DialogResult.OK)
   {
          RefreshDataGrid();
   }
   else
   {
     //it was canceled
   }
   editFields.Dispose();

You need to pass the main form to the detail form. 您需要将主表单传递给详细信息表单。
In the detail form, implement the OnClosing or OnClosed event and call MainForm.LoadList() . 在详细信息表单中,实现OnClosingOnClosed事件,然后调用MainForm.LoadList()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM