仅在关闭和打开对话框后更新数据库 [英] Database only updates after closing and opening dialog

查看:45
本文介绍了仅在关闭和打开对话框后更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在窗体中有一个DataGridView,每次加载这样的页面时,该窗体都会显示数据库中的数据:

I have a DataGridView inside a form that displays data from a database every time I load the page like this:

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);
}

当我双击列表中的一个条目时,将打开一个对话框包含表单并从该条目加载相应的详细信息。当我保存详细信息时,对话框关闭,并且在我的DataGridView所在的类中,有一个FormClose函数可以刷新DataGridView。

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.

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();
}

但是,我遇到的问题是DataGridView正确刷新的唯一方法如果我关闭了我所在的主窗体,然后再次将其重新打开。

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.

我调试并设法捕获了一些结果。

I debugged and managed to capture some results.

Image1:紧接在更新表单关闭之后。在字段列表中,只有一个条目可以具有IsPrint = true。但是图像显示这两个都是正确的。

Image1: Directly after the Update form is closed. In the fields list, only one entry can have IsPrint = true. However the image shows that both are true.

Image2:关闭并重新打开包含DataGridView的页面后,它将显示此正确结果。只有1 IsPrint = true。

Image2: After I close and reopen the page containing the DataGridView, it shows this correct result. Only 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();

编辑:

我添加了一个对话框结果检查,但仍然无法正确更新datagridview。

I added a dialogresult check but it still doesn't update the datagridview properly. Maybe its a thread issue?

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


推荐答案

我只是假设OnCklick事件看起来像在您打开对话框的位置,但是我认为问题是您试图从另一个Form / Thread刷新Datagrid。 (这将不起作用)

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. (which will not work)

我建议您使用ShowDialog打开对话框表单,并在onclick事件本身关闭窗体后刷新网格。尝试替换

I suggest you open the dialog form by using ShowDialog and refresh the grid after the Form was closed in the onclick event itself. Try replacing

RefreshDataGrid();

在您的FormClosed事件中,

in your FormClosed event with

DialogResult = DialogResult.OK;

然后,您可以在onclick事件中处理网格重新加载,如下所示:

Then you are able to handle the grid reloading in your onclick event like this:

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

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

这篇关于仅在关闭和打开对话框后更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆