Hi there,
I have a DataGridView with a BindingSource of products. This products have an enum (Producer).
For the most text fields (to edit the product) below the DataGridView I have a method RefreshProduct which does a ResetBindings in the end to refresh the DataGridView. There is a ComboBox (cboProducer), too.
If I run over the _orderBs.ResetBindings(false) it will reset my cboProducer outside the DataGridView, too. Could you please help me to avoid this?
Here follows some code; maybe it is then better to understand.
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);
}
}