Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中将数据绑定到combobox#_C#_Visual Studio 2012_Psql - Fatal编程技术网

C# 在C语言中将数据绑定到combobox#

C# 在C语言中将数据绑定到combobox#,c#,visual-studio-2012,psql,C#,Visual Studio 2012,Psql,我正在尝试使用c#和psql将数据绑定到组合框。但是,当我向数据库添加数据时,我无法使组合框自动更新 以下是我将组合框绑定到数据库的方式: private void bindProductComboBox(ComboBox box) { box.DataSource = allProducts; box.ValueMember = "product_id"; box.DisplayMember = "name"; } allPro

我正在尝试使用c#和psql将数据绑定到组合框。但是,当我向数据库添加数据时,我无法使组合框自动更新

以下是我将组合框绑定到数据库的方式:

private void bindProductComboBox(ComboBox box)
    {
        box.DataSource = allProducts;
        box.ValueMember = "product_id";
        box.DisplayMember = "name";
    }
allProducts是一个私有数据表,用于接收数据库中的所有产品

这是我的ProductModel课程

public class ProductsModel : INotifyPropertyChanged
{

    private DatabaseConnector db = new DatabaseConnector();
    private DbConnection conn = null;
    private Helper helper = new Helper();

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged([CallerMemberName] String caller = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(caller));
        }
    }

    public void Add(String partCode, String name, Int16 supplier, Decimal unitCost)
    {
        using (this.conn = this.db.OpenConnection())
        {
            string statement = "INSERT INTO products ( part_code, name, supplier, unit_cost )" +
                               "VALUES ( @partCode, @name, @supplier, @unitCost )";
            DbCommand cmd = this.helper.initializeCommand(this.conn, statement);

            this.helper.createStringParam(cmd, "@partCode", partCode);
            this.helper.createStringParam(cmd, "@name", name);
            this.helper.createInt16Param(cmd, "@supplier", supplier);
            this.helper.createDecimalParam(cmd, "@unitCost", unitCost);

            cmd.Prepare();
            cmd.ExecuteNonQuery();
            RaisePropertyChanged();
        }
    }

    public DataTable GetAll()
    {
        using (this.conn = this.db.OpenConnection())
        {
            String statement = "SELECT * FROM products";
            DbCommand cmd = this.helper.initializeCommand(this.conn, statement);

            cmd.Prepare();
            DbDataReader reader = cmd.ExecuteReader();

            DataTable table = new DataTable();
            table.Columns.Add("product_id", typeof(Int16));
            table.Columns.Add("name", typeof(String));
            table.Load(reader);

            return table;
        }
    }
}

我不确定是否正确实现了INotifyPropertyChanged接口。

在向数据库输入数据时引发属性更改事件不会神奇地让UI知道它应该更新值。您可能需要再次调用
GetAll()
,并将UI绑定到这些值。这是否意味着每次添加到数据库时都必须更改数据源?这就是我现在正在做的一种创可贴解决方案,但我希望能有更好的方法来做。