0

I am making an inventory project, users can pick the category, put price, quantity, etc. but with customized exceptions. I'm trying to put all of these data into the DataGridView. I've checked and supposedly I should Declare the variable showProductList of BindSourceclass inside frmAddProduct. Instantiate the BindingSource class inside the constructor of frmAddProduct(). But how exactly? (The error comes from showProductList, I think I am missing something, but I don't know what.)

this is what I have tried:

public partial class frmAddProduct : Form
{
    private string _ProductName, _Category, _MfgDate, _ExpDate, _Description;
    private int _Quantity;
    private double _SellPrice;

    public string Product_Name (string name)
    {
        if (!Regex.IsMatch(name, @"^[a-zA-Z]+$"))



            try
            {
                
            }
            catch (Exception StringFormattException)
            {
                
            }
            finally
            {
                
            }


        return name;
    }

    public int Quantity(string qty)
    {
        if (!Regex.IsMatch(qty, @"^[0-9]"))


            try
            {
                
            }
            catch (Exception NumberFormattException)
            {
               
            }
            finally
            {
               
            }


        return Convert.ToInt32(qty);
    }
    public double SellingPrice(string price)
    {
        if (!Regex.IsMatch(price.ToString(), @"^(\d*\.)?\d+$"))

            try
            {
               
            }
            catch (Exception CurrencyFormatException)
            {
                
            }
            finally
            {
                
            }

        return Convert.ToDouble(price);
    }

    public frmAddProduct()
    {
        InitializeComponent();

        var showProductList = new BindingSource();

    }

    private void frmAddProduct_Load(object sender, EventArgs e)
    {
        string[] ListOfProductCategory = { "Beverages", "Bread/Bakery", "Canned/Jarred Goods", "Dairy", "Frozen Goods", "Meat", "Personal Care", "Other" };

        foreach(string LPC in ListOfProductCategory)
        {
            cbCategory.Items.Add(LPC);
        }
    }



    private void btnAddProduct_Click(object sender, EventArgs e)
    {
        _ProductName = Product_Name(txtProductName.Text);
        _Category = cbCategory.Text;
        _MfgDate = dtPickerMfgDate.Value.ToString("yyyy-MM-dd");
        _ExpDate = dtPickerExpDate.Value.ToString("yyyy-MM-dd");
        _Description = richtxtDescription.Text;
        _Quantity = Quantity(txtQuantity.Text);
        _SellPrice = SellingPrice(txtSellPrice.Text);
        showProductList.Add(new ProductClass(_ProductName, _Category, _MfgDate, _ExpDate, _SellPrice, _Quantity, _Description));
        gridViewProductList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
        gridViewProductList.DataSource = showProductList;



    }

}

this is the other class:

 class ProductClass
{
    private int _Quantity;
    private double _SellingPrice;
    private string _ProductName, _Category, _ManufacturingDate, _ExpirationDate, _Description;

    public ProductClass(string ProductName, string Category, string MfgDate, string ExpDate, double Price, int Quantity, string Description)
    {
        this._Quantity = Quantity;
        this._SellingPrice = Price;
        this._ProductName = ProductName;
        this._Category = Category;
        this._ManufacturingDate = MfgDate;
        this._ExpirationDate = ExpDate;
        this._Description = Description;
    }

    public string productName
    {
        get
        {
            return this._ProductName;
        }
        set
        {
            this._ProductName = value;
        }
    }

    public string Category
    {
        get
        {
            return this._Category;
        }
        set
        {
            this._Category = value;
        }
    }

    public string manufacturingDate
    {
        get
        {
            return this._ManufacturingDate;
        }
        set
        {
            this._ManufacturingDate = value;
        }
    }

    public string expirationDate
    {
        get
        {
            return this._ExpirationDate;
        }
        set
        {
            this._ExpirationDate = value;
        }
    }

    public string description
    {
        get
        {
            return this._Description;
        }
        set
        {
            this._Description = value;
        }
    }

    public int quantity
    {
        get
        {
            return this._Quantity;
        }
        set
        {
            this._Quantity = value;
        }
    }

    public double sellingPrice
    {
        get
        {
            return this._SellingPrice;
        }
        set
        {
            this._SellingPrice = value;
        }
    }



    class NumberFormattException : Exception 
    { 
        public NumberFormattException(string Quantity) : base(Quantity) { } 
    }

    class StringFormattException : Exception
    {
        public StringFormattException(string Product_Name) : base(Product_Name) { }
    }
    class CurrencyFormatException : Exception
    {
        public CurrencyFormatException( string SellingPrice) : base(SellingPrice) { }
    }







}
2
  • I am just trying to see what the code is doing and forgive me if I am missing something, however, what is the purpose of the first three methods: public string Product_Name (string name). public int Quantity(string qty) and public double SellingPrice(string price) ? … They appear to do nothing in addition to using the “Console” to get data from the user…? This is odd to use the Console to get data when this is a winforms app. Can you clarify? Commented Dec 12, 2020 at 11:47
  • Sorry, I forgot to edit that out. I was trying stuff from tutorials to see what would get rid of some errors. It was supposed to be for the custom exceptions. Commented Dec 12, 2020 at 12:20

1 Answer 1

2

This line of code is not doing what you think it is doing…

showProductList.Add(new ProductClass(_ProductName, _Category, _MfgDate, _ExpDate, _SellPrice, _Quantity, _Description));

showProductList is a BindingSource and you need to set its DataSource property.

A BindingList<ProductClass> or a List<ProductClass> or even a DataTable will work as a DataSource for the BindingSource.

With this new “list”, the code “adds” the items to this list, then simply calls the BindingSources.ResetBindings(false) to update the grid.

Example:

Add these global variables to the form…

BindingSource ProductsBS;
BindingList<ProductClass> ListOfProducts;

Update the forms constructor to initialize the binding source and set its data source….

public Form1() {
  InitializeComponent();
  ProductsBS = new BindingSource();
  ListOfProducts = new BindingList<ProductClass>();
  ProductsBS.DataSource = ListOfProducts;
  gridViewProductList.DataSource = ProductsBS;
  gridViewProductList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
}

Finally, change the button click event code like…

private void btnAddProduct_Click(object sender, EventArgs e) {
  _ProductName = Product_Name(txtProductName.Text);
  _Category = cbCategory.Text;
  _MfgDate = dtPickerMfgDate.Value.ToString("yyyy-MM-dd");
  _ExpDate = dtPickerExpDate.Value.ToString("yyyy-MM-dd");
  _Description = richtxtDescription.Text;
  _Quantity = Quantity(txtQuantity.Text);
  _SellPrice = SellingPrice(txtSellPrice.Text);
  ListOfProducts.Add(new ProductClass(_ProductName, _Category, _MfgDate, _ExpDate, _SellPrice, _Quantity, _Description));
  ProductsBS.ResetBindings(false);
}

I hope this makes sense.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.