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) { }
}
}
public string Product_Name (string name).public int Quantity(string qty)andpublic 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?