【问题标题】:DataGridView Not Updating in C# When Binding Source is Correct当绑定源正确时,DataGridView 在 C# 中不更新
【发布时间】:2018-07-21 18:29:36
【问题描述】:

我有一个 DataGridView 和一个编辑按钮。当我单击编辑按钮并更改字段时,我按保存,DataGridView 再次出现。问题是我的更改没有显示出来。

使用调试器,我在 DGV 和 BindingSource 中查看了正确的数据。它只是没有显示在 DGV 中。

这是我的代码 - 我确实意识到它是半冗余的,但在这一点上,我已经完成了四个小时,并且愿意尝试任何事情。

        this.iSOXTableAdapter.FillByISOX(this.MSDataSet.ISOX);

        BindingSource bindingSource = new BindingSource();
        bindingSource.DataSource = iSOXBindingSource;
        iSOXDataGridView.DataSource = null;
        iSOXDataGridView.DataSource = bindingSource;
        bindingSource.ResetBindings(false);
        this.iSOXDataGridView.Refresh();

我查看了以下问题(以及许多其他问题)并尝试了他们的建议,但无济于事:

Datagridview not updating correctly

dataGridView not updating c#?

DataGridView not updating in c#

Best way to refresh DataGridView when you update the base data source

How to refresh or show immediately in datagridview after inserting?

感谢您对解决方法的任何帮助、建议或想法。非常感谢你看这个。

********************** 编辑 *******************

这是保存按钮的代码 - 我知道它正在工作,因为在我重新查询数据后,它位于绑定源和 DGV 中。此代码在单独的添加/编辑表单中:

  private void BtnSave_Click(object sender, EventArgs e)
    {
        if (ValidateForm())
        {
            ISOXBindingNavigatorSaveItem_Click(sender, e);
            this.Close();
        }
        else
        {
            MessageBox.Show("Not Validated - Could not Save");
        }

    }

下面是带有 DGV 的用户控件的完整代码:

public partial class FindISOXControl : UserControl
{
    private bool gridInitialized = false;

    public delegate void ItemHasBeenSelected(object sender, SelectedItemEventArgs e);
    public event ItemHasBeenSelected SelectedItem;

    public class SelectedItemEventArgs : EventArgs
    {
        public int SelectedChoice { get; set; }
    }

    public bool First = true;

    public FindISOXControl()
    {            
        InitializeComponent();
        FillTableAdapter();
        iSOXDataGridView.Columns.Cast<DataGridViewColumn>().ToList().ForEach(f => f.SortMode = DataGridViewColumnSortMode.NotSortable);

    }

    public void FillTableAdapter()
    {
        this.iSOXTableAdapter.FillByISOX(this.MSDataSet.ISO);

        BindingSource bindingSource = new BindingSource();
        bindingSource.DataSource = iSOXBindingSource;
        iSOXDataGridView.DataSource = null;
        iSOXDataGridView.DataSource = bindingSource;
        bindingSource.ResetBindings(false);
        this.iSOXDataGridView.Refresh(); 
        setGridData(); 
    }
    public void UpdateISOXText(string pISOX = "")
    {
        this.txtFind.Text = pISOX;
        txtFind.Refresh();
    }
    DataTable dt = new DataTable();
    public void btnFind_Click(object sender, EventArgs e)
    {

        {
            setGridData();
        }
    }
    public void setGridData()
    {
        GetData(); 
        if (iSOXDataGridView.RowCount > 0)
        {
            EventArgs e = new EventArgs();
            iSOXDataGridView_SelectionChanged(null, e);
        }
    }
    public void txtISOX_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Tab)
        {
            setGridData();
        }
    }
    //Query database
    public void GetData()
    {
        String searchValue = txtFind.Text.Trim().ToUpper();
        int rowIndex = -1;
        foreach (DataGridViewRow row in iSOXDataGridView.Rows)
        {
            if (row.Cells[0].Value.ToString().Contains(searchValue))
            {
                rowIndex = row.Index;
                break;
            }
        }
        if (rowIndex == -1)
        {
            foreach (DataGridViewRow row in iSOXDataGridView.Rows)
            {
                if (row.Cells[4].Value.ToString().ToUpper().Contains(searchValue))
                {
                    rowIndex = row.Index;
                    break;
                }

            }
        }
        if (rowIndex == -1)
        {
            if (searchValue != null && searchValue !="")
            { 
                MessageBox.Show(searchValue + " Not Found");
            }
        }
        else
        {
            iSOXDataGridView.Rows[rowIndex].Selected = true;
            iSOXDataGridView.CurrentCell = iSOXDataGridView.Rows[rowIndex].Cells[0];
        }

    }


    public void iSOXDataGridView_SelectionChanged(object sender, EventArgs e)
    {
        if (iSOXDataGridView.CurrentRow != null)
        {
            int Row = iSOXDataGridView.CurrentRow.Index;
            if (gridInitialized)
            {
                txtFind.Text = iSOXDataGridView[0, Row].Value.ToString();
                // 6 is the ID column
                DataGridViewCellEventArgs ev = new DataGridViewCellEventArgs(6, Row);
                iSOXDataGridView_CellDoubleClick(sender, ev);
            }
        }
    }
    private void iSOXDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {

        if (e.RowIndex >= 0 && e.RowIndex < iSOXDataGridView.RowCount)
        {
            // 6 == id column
            int choice = (int)iSOXDataGridView[6, First ? 0 : e.RowIndex].Value;
            this.SelectedItem(this, new SelectedItemEventArgs { SelectedChoice = choice });

        }
    }

    private void iSOXDataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
    {
        // 6 is the ID column
        DataGridViewCellEventArgs ev = new DataGridViewCellEventArgs(6, e.RowIndex);
        if (e.RowIndex != 0)
        {
            First = false;
            iSOXDataGridView_CellDoubleClick(sender, ev);
        }
    }
    private void iSOXDataGridView_RowLeave(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex == 0 && e.ColumnIndex == 0)
        { First = true; }
    }
}

这是从编辑返回后带有用户控件的主窗体的代码:

   uc.FillTableAdapter(); 

********* EDIT --> 打开编辑表单的代码:

            AddEditISOX aeix = new AddEditISOX("E", currentISOX);
            aeix.ShowDialog();
            ISOX_Load(sender, e);

           ISOX_Load() calls  uc.FillTableAdapter();

【问题讨论】:

  • 你能发布完整的代码吗?
  • 考虑发布相关的编辑/保存按钮代码。发布的绑定代码看起来不错 - 但它的放置位置/运行时间可能很重要。
  • 代码是单独的形式?您如何将数据集实例相关数据传递给它?
  • 我认为你需要调用 AcceptChanges();在您的数据表上。
  • 不清楚你在做什么/你在问什么。你的代码有很多奇怪的东西,比如直接调用事件处理程序方法。请记住,在保存数据之前,您需要结束当前编辑并将挂起的更改应用到基础数据源。在您的情况下,由于您使用的是BindingSource,因此您应该在保存之前调用BindingSource 的EndEdit 方法:this.Validate(); this.bindingSource1.EndEdit(); this.tableAdapterManager.UpdateAll(this.myDataSet);

标签: c# winforms datagridview


【解决方案1】:

第一次分配 DataSource 时,DataGridView 会设置绑定。问题是后续的 DataSource 分配,如果分配的结构与初始分配不同,将会失败,因为绑定现在“关闭”

您需要重置 DataGridView thusly 以便将数据绑定到新的。 (该链接适用于 VB,但您只需要知道要调用的方法即可。即使复制/粘贴也会过大。)

【讨论】:

  • 我不知道其他 .net 版本是否有所不同,但对于我使用的 4.6.1,我从不重置 datagridview。并且绑定在只有一个控件上总是不同的。它有效
  • 链接没有显示答案:(它说这个话题已经死了六个多月。你能发布你找到的答案吗?谢谢。
【解决方案2】:

也许您在适配器中没有正确的更新 sql 命令 将这些命令添加到您的初始化方法中

iSOXTableAdapter.Adapter.MissingSchemaAction = MissingSchemaAction.Error;
iSOXTableAdapter.Adapter.MissingMappingAction = MissingMappingAction.Error;

也许生成的错误会告诉你那里缺少什么

【讨论】:

    【解决方案3】:

    将您的数据绑定到表视图,它会自动获取更新。

    【讨论】:

    • 不是那么多——数据已绑定,但仍然无法正常工作。感谢您考虑到这一点。不过我会再次检查我的代码。
    【解决方案4】:

    我终于想通了这个该死的事情。在我的 Edit Button Click 方法中,我需要再次调用用户控件填充表适配器方法。

         private void BtnEdit_Click(object sender, EventArgs e)
        {
            if (currentISOX != 0)
            {
                AddEditISO aei = new AddEditISOX("E", currentISOX);
                aei.ShowDialog();
                findISOXControl1.FillTableAdapter(); 
    
            }
            else
            {
                MessageBox.Show("Please Make a Selection First");
            }
        }
    

    感谢大家的想法、建议和意见。这非常有帮助,最终引导我找到了解决方案。

    【讨论】:

      猜你喜欢
      • 2011-02-02
      • 2011-07-28
      • 2014-09-15
      • 2013-06-10
      • 2017-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      相关资源
      最近更新 更多