【发布时间】:2021-03-23 15:01:00
【问题描述】:
我正在做一个库存项目,用户可以选择类别、放置价格、数量等,但自定义例外。我正在尝试将所有这些数据放入 DataGridView。我已经检查过,据说我应该在 frmAddProduct 中声明 BindSourceclass 的变量 showProductList。在 frmAddProduct() 的构造函数中实例化 BindingSource 类。 但具体如何? (错误来自showProductList,我想我错过了什么,但我不知道是什么。)
这是我尝试过的:
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;
}
}
这是另一个类:
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)和public double SellingPrice(string price)? ......除了使用“控制台”从用户那里获取数据之外,他们似乎什么都不做......?当这是一个 winforms 应用程序时,使用控制台获取数据很奇怪。你能澄清一下吗? -
抱歉,我忘记编辑了。我正在尝试教程中的东西,看看什么可以消除一些错误。它应该用于自定义异常。
标签: c# winforms exception datagridview datagrid