【问题标题】:How do I set a different data source for the DataGridView using BindSource?如何使用 BindSource 为 DataGridView 设置不同的数据源?
【发布时间】: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


【解决方案1】:

这行代码没有做你认为它正在做的事情……

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

showProductList 是一个BindingSource,您需要设置它的DataSource 属性。

BindingList<ProductClass> 或 List<ProductClass> 甚至 DataTable 将作为 DataSource 用于 BindingSource。

使用这个新的“列表”,代码将项目“添加”到这个列表中,然后简单地调用BindingSources.ResetBindings(false) 来更新网格。

例子:

将这些全局变量添加到表单中……

BindingSource ProductsBS;
BindingList<ProductClass> ListOfProducts;

更新表单构造函数以初始化绑定源并设置其数据源……

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

最后,将按钮点击事件代码更改为……

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);
}

我希望这是有道理的。

【讨论】:

    猜你喜欢
    • 2014-01-14
    • 1970-01-01
    • 2010-10-01
    • 2021-01-10
    • 2013-03-26
    • 2020-09-13
    • 1970-01-01
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多