关闭子窗体时,C#DataGridView从数据库中检索过时的结果

时间:2019-02-26 13:38:02

标签: c# .net datagridview

我有一个表单打开了一个包含DataGridView的对话框

当我双击DataGridView时,它将打开一个包含该行详细信息的子窗体。

保存所有更新后,子窗体将关闭,DataGridView应该刷新。

但是,我注意到DataGridView的刷新功能没有从数据库中获取正确的数据。它是从更新之前而不是更新之后获取数据,从而显示错误的数据。

我不确定如何从这里继续,因为我当前的方法是直接从子窗体中刷新DataGridView。

这是我的代码

这是打开子窗体的双击动作

private void fieldsDataGridView_DoubleClick(object sender, EventArgs e)
{
    foreach (DataGridViewRow row in fieldsDataGridView.SelectedRows)
    {
        fieldIdInput.Text = row.Cells["Id"].Value.ToString();
        DialogResult dr = new DialogResult();
        EditFields editFields = new EditFields(this,programIdInput.Text, fieldIdInput.Text, false);
        editFields.Show();
    }
}

这是第一个表格中包含的“刷新”功能

public 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.Refresh();
    fieldsDataGridView.ClearSelection();
}

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

这是子窗体的构造函数

private string ProgramId;
private string FieldId;
private bool IsNew;
private Fields Field;
ProgramsData programsData = new ProgramsData();
FieldsData fieldsData = new FieldsData();
PrinterData printerData = new PrinterData();

public EditFields(Fields Field, string ProgramId, string FieldId, bool IsNew)
{
    this.ProgramId = ProgramId;
    this.FieldId = FieldId;
    this.IsNew = IsNew;
    this.Field = Field;
    InitializeComponent();
    this.FormClosing += new FormClosingEventHandler(this.EditFields_FormClosing);
}

这是子窗体关闭功能

private void EditFields_FormClosing(object sender, FormClosingEventArgs e)
{
    Field.RefreshDataGrid();
}

0 个答案:

没有答案