Пример #1
0
        private void dgvProductCategory_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            try
            {
                if (!dgvProductCategory.IsCurrentRowDirty)
                {
                    return;
                }

                if (dgvProductCategory.CurrentRow.Cells["ProductCategoryCode"].Value != null)
                {
                    SqlString productCategoryCode = dgvProductCategory.CurrentRow.Cells["ProductCategoryCode"].Value.ToString();
                    if (productCategoryCode.IsNull)
                    {
                        dgvProductCategory.Rows[e.RowIndex].ErrorText = "Product Category Code is required";
                        e.Cancel = true;
                        return;
                    }
                    var rx = new System.Text.RegularExpressions.Regex(@"^\d{2}$");
                    if (!rx.IsMatch(productCategoryCode.Value))
                    {
                        dgvProductCategory.Rows[e.RowIndex].ErrorText = "Please enter Product Category Code in ##";
                        e.Cancel = true;
                        return;
                    }
                    ProductCategory productCategory = null;
                    // new record
                    if (e.RowIndex >= _productCategoryCollection.Count)
                    {
                        var checkProductCategoryCollection = new ProductCategory.ParameteredCollection(productCategoryCode, SqlString.Null);
                        checkProductCategoryCollection.Load();
                        if (checkProductCategoryCollection.Count > 0)
                        {
                            dgvProductCategory.Rows[e.RowIndex].ErrorText = "Product Category already exists";
                            e.Cancel = true;
                        }
                        else
                        {
                            productCategory = _productCategoryCollection.Create(new ProductCategoryKey(productCategoryCode));
                            if (dgvProductCategory.CurrentRow.Cells["ProductCategoryDescription"].Value != null)
                            {
                                productCategory.ProductCategoryDescription = (SqlString)(dgvProductCategory.CurrentRow.Cells["ProductCategoryDescription"].Value);
                            }
                            _productCategoryCollection.Save(productCategory);
                            // when the initial collection is empty
                            // 1. productCategory in bindingSource is not set to the correct type, so reset the bindingSource.DataSource below
                            // 2. the data moves above need to check for null
                            if (bindingSourceProductCategory.Count == 1)
                            {
                                bindingSourceProductCategory.DataSource = null;
                                bindingSourceProductCategory.DataSource = _productCategoryCollection;
                            }
                            else
                            {
                                bindingSourceProductCategory.List[bindingSourceProductCategory.Count - 1] = productCategory;
                            }
                        }
                    }
                    // existing record
                    else
                    {
                        //productCategory = _productCategoryCollection[new ProductCategoryKey(productCategoryCode)];
                        //productCategory.Save();
                        productCategory = (ProductCategory)dgvProductCategory.CurrentRow.DataBoundItem;
                        _productCategoryCollection.Save(productCategory);
                        _productCategoryCollection[new ProductCategoryKey(productCategoryCode)] = productCategory;
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.GetInstance().HandleException(this, ex, e);
            }
        }