绑定源正确时,DataGridView不在C#中更新

时间:2018-02-11 13:34:32

标签: c# winforms datagridview

我有一个DataGridView和一个Edit按钮。单击“编辑”按钮并更改字段时,按“保存”,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 - &gt;正在打开编辑表单的代码:

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

           ISOX_Load() calls  uc.FillTableAdapter();

4 个答案:

答案 0 :(得分:1)

DataGridView在您第一次分配DataSource时设置绑定。问题是后续的DataSource分配,如果赋值与初始赋值具有不同的结构,将会失败,因为绑定现在是&#34; off&#34;

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

答案 1 :(得分:1)

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

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

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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

我终于想出了这个爆炸的东西。在我的编辑按钮单击方法中,我需要再次调用用户控件填充表适配器方法。

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

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