jeudi 19 décembre 2019

DataGridView doesn't show actual value of checkboxcolumn

I read data from database and bind it to a List

private void SetDataSource
{
    List<Warehouse> list = ListWarehouse(idINV); // DB query.
    m_WarehouseBindingList = new BindingList<Warehouse>(list);
    m_WarehouseBindingSource.DataSource = m_WarehouseBindingList;
}

Bind it to the DataGridView

private void LoadDataGridView()
        {
            if (m_WarehouseBindingSource== null) { return; }
            if (dgWarehouse.DataSource != null)
            {
                dgWarehouse.DataSource = null;
                dgWarehouse.Rows.Clear();
                dgWarehouse.Columns.Clear();
            }
            dgWarehouse.AutoGenerateColumns = true;
            dgWarehouse.DataSource = m_WarehouseBindingSource;
}

And set DataBinding to Textbox (and Checkbox)

private void SetDataBinding()
        {

tbAmount.DataBindings.Clear();
            tbAmount.DataBindings.Add("Text", m_WarehouseBindingSource, "Amount", true, DataSourceUpdateMode.OnPropertyChanged);
 chkCounted.DataBindings.Clear();
            chkCounted.DataBindings.Add("Checked", m_WarehouseBindingList, "Counted", true, DataSourceUpdateMode.OnPropertyChanged);

            _warehouse = null; // Object of class Warehouse.
            _warehouse = (Warehouse)m_WarehouseBindingSource.Current;
        }

Now I enter a value in tbAmount and save the data.

private void tbAmount_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                    if (sender is TextBox)
                    {
                        TextBox tb = (TextBox)sender;

                        var v = new DoubleValidator();
                        if (!v.IsValid(tb.Text.ToString()))
                        {
                            MessageBox.Show("Enter double value");
                            tb.Focus();
                        }
                        else
                        {
                            try
                            {
                                Cursor.Current = Cursors.WaitCursor;

                                if (_warehouse != null)
                                {
                                    SaveAmount(_warehouse.IdINV, _warehouse.Amount, true);
                                    m_BestandBindingSource.MoveNext();
                                }
                            }
                            finally
                            {
                                tbAmount.Focus();
                                tbAmount.Text = "";
                                SetDataBinding();
                                Cursor.Current = Cursors.Default;
                            }
                        }
                    }
                }

            }

Now I've got the problem that the "Counted" Column will not be updated. For the amount value everything works fine.

What is wrong? How to solve the problem?




Aucun commentaire:

Enregistrer un commentaire