3
votes

I have a DataGridView and an Edit button. When I click the Edit button and change a field, I press Save and the DataGridView comes up again. The problem is that my changes aren't showing.

Using the debugger, I have looked in the DGV and in the BindingSource and the correct data is there. It is just not displaying in the DGV.

Here is my code - I do realize that it is semi-redundant but, at this point, I'm four hours into it and am willing to try anything.

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

I have looked at the following questions (and many others) and tried their suggestions to no avail:

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?

I appreciate any help or suggestions or ideas for workarounds. Thank you so much for looking at this.

***************** EDIT *******************

Here is the code for the save button - I know it is working because after I requery the data, it is in the binding source and in the DGV. This code is in a separate add/edit form:

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

    }

Here is full code for the user control with the DGV on it:

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

Here is the code from the main form with the user control on it after it returns from the edit:

   uc.FillTableAdapter(); 

********* EDIT --> Code that is opening Edit form:

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

           ISOX_Load() calls  uc.FillTableAdapter();
4
Could you post full code? - Aleksa Ristic
Consider posting the relevant Edit/Save button code. Posted binding code seems OK - but could be important where it is placed / when it runs. - user6144226
Code is in a separate form? How are you passing your dataset instances relevant data to it? - user6144226
I think you need to call AcceptChanges(); on your DataTable. - Berkay
It's unclear what you are doing/what you are asking. You code has lots of strange things like calling event handler methods directly. Just keep in mind, before saving data, you need to end current edit and apply pending changes to the underlying data source. In your case, since you are using BindingSource, you should call EndEdit method of the BindingSource before save: this.Validate(); this.bindingSource1.EndEdit(); this.tableAdapterManager.UpdateAll(this.myDataSet); - Reza Aghaei

4 Answers

1
votes

A DataGridView sets up bindings the first time you assign the DataSource. The problem is that subsequent DataSource assignments, if the assignments have a different structure from the initial assignment, will fail because the bindings are now "off"

You need to reset the DataGridView thusly so that the data is bound a new. (The link is for VB but you just need to know the methods to call. Even copy/paste would be overkill.)

1
votes

Maybe you don't have proper update sql commands in the adapter add these commands to your intialize method

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

maybe the generated error will tell you what are you missing there

1
votes

bind your data to the View of table and it would automatically get updates.

0
votes

I finally figured this blasted thing out. In my Edit Button Click method, I need to call the user control fill table adapter method again.

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

Thanks for everyone's thoughts, advice and input. It was very helpful and eventually led me to the solution.