Grid的BindingSource上的ResetBindings也会重置ComboBox

时间:2011-03-17 08:06:27

标签: c# winforms datagridview enums

我有一个带有BindingSource产品的DataGridView。这个产品有一个枚举(制片人)。

对于DataGridView下面的大多数文本字段(编辑产品),我有一个方法RefreshProduct,它最后执行一个ResetBindings来刷新DataGridView。还有一个ComboBox(cboProducer)。

如果我遍历_orderBs.ResetBindings(false),它也将重置我在DataGridView之外的cboProducer。你能帮我避免这个吗?

下面是一些代码;或许更好的理解。

public partial class SelectProducts : UserControl
    {
        private AutoCompleteStringCollection _productCollection;
        private ProductBL _productBL;
        private OrderBL _orderBL;
        private SortableBindingList<ProductBE> _listProducts;
        private ProductBE _selectedProduct;
        private OrderBE _order;
        BindingSource _orderBs = new BindingSource();

        public SelectProducts()
        {
            InitializeComponent();

            if (_productBL == null)
                _productBL = new ProductBL();

            if (_orderBL == null)
                _orderBL = new OrderBL();

            if (_productCollection == null)
                _productCollection = new AutoCompleteStringCollection();

            if (_order == null)
                _order = new OrderBE();

            if (_listProducts == null)
            {
                _listProducts = _order.ProductList;
                _orderBs.DataSource = _order;
                grdOrder.DataSource = _orderBs;
                grdOrder.DataMember = "ProductList";
            }
        }

        private void cmdGetProduct_Click(object sender, EventArgs e)
        {
            ProductBE product = _productBL.Load(txtProductNumber.Text);
            _listProducts.Add(product);

            _orderBs.ResetBindings(false);
        }

        private void grdOrder_SelectionChanged(object sender, EventArgs e)
        {
            if (grdOrder.SelectedRows.Count > 0)
            {
                _selectedProduct = (ProductBE)((DataGridView)(sender)).CurrentRow.DataBoundItem;
                if (_selectedProduct != null)
                {
                    txtArticleNumber.Text = _selectedProduct.Article;
                    txtPrice.Text = _selectedProduct.Price.ToString("C");
                    txtProducerNew.Text = _selectedProduct.ProducerText;
                    cboProducer.DataSource = Enum.GetValues(typeof(Producer));
                    cboProducer.SelectedItem = _selectedProduct.Producer;
                }
            }
        }

        private void txtProducerNew_Leave(object sender, EventArgs e)
        {
            string property = CommonMethods.GetPropertyName(() => new ProductBE().ProducerText);
            RefreshProduct(((TextBoxBase)sender).Text, property);
        }

        private void RefreshProduct(object value, string property)
        {
            if (_selectedProduct != null)
            {
                double valueOfDouble;
                if (double.TryParse(value.ToString(), out valueOfDouble))
                {
                    value = valueOfDouble;
                }

                Type type = _selectedProduct.GetType();
                PropertyInfo info = type.GetProperty(property);

                if (info.PropertyType.BaseType == typeof(Enum))
                {
                    value = Enum.Parse(info.PropertyType, value.ToString()); 
                }

                try
                {
                    Convert.ChangeType(value, info.PropertyType, new CultureInfo("de-DE"));
                    info.SetValue(_selectedProduct, value, null);
                }                    
                catch (Exception ex)
                {
                    throw new WrongFormatException("\"" + value.ToString() + "\" is not a valid value.", ex);
                }


                var produktFromList = _listProducts.Single(p => p.Position == _selectedProduct.Position);
                info.SetValue(produktFromList, value, null);

                _orderBs.ResetBindings(false);
            }
        }

        private void cboProducer_SelectedIndexChanged(object sender, EventArgs e)
        {
            var selectedIndex = ((ComboBox)(sender)).SelectedIndex;
            switch ((Producer)selectedIndex)
            {
                case Producer.ABC:
                    txtProducerNew.Text = Constants.ABC;
                    break;
                case Producer.DEF:
                    txtProducerNew.Text = Constants.DEF;
                    break;
                case Producer.GHI:
                    txtProducerNew.Text = Constants.GHI;
                    break;
                case Producer.Another:
                    txtProducerNew.Text = String.Empty;
                    break;
                default:
                    break;
            }

            string property = CommonMethods.GetPropertyName(() => new ProductBE().Producer);
            RefreshProduct(selectedIndex, property);
        }
    }

1 个答案:

答案 0 :(得分:0)

我明白了! 在ResetBindings之后,引发了grdOrder_SelectionChanged-Event。在_selectedProduct为null之后(我还不知道为什么......)再次设置了cboProducer的DataSource。

因此,解决方案是在构造函数中设置cboProducer的DataSource。

感谢阅读。