将具有属性和列表的类绑定到WinForms DatagridView

时间:2017-07-11 13:16:32

标签: c# forms winforms datagridview bindinglist

我遇到了以下问题:

我有一个包含Produkt和Gruppe的BindingLists的Core-Class作为公共静态。

类核心:

    private static BindingList<Produkt> _produkte;
    private static BindingList<Gruppe> _gruppen;
    private static BindingList<Tag> _tage;
 public Core() //operating class
    {
        Produkte = new BindingList<Produkt>();
        Gruppen = new BindingList<Gruppe>();
        Tage = new BindingList<Tag>();
    }

Class Produkt:

    private int _id; //Product attributes
    private string _name;
    private Gruppe _gruppe;
    private double _kohlenhydrate_je_100g;
    private double _eiweiss_je_100g;
    private double _fett_je_100g;
    private double _alkoholgehalt;
    private double _kcal;

Class Gruppe:

    private int _id; //group attributes. Each product has got 0-1 group
    private string _name;

每个变量都有一个属性,包含公共Set&amp;得到。 我需要在另一个表单DataGridView中显示BindingList _produkte,但我无法用Gruppe的所有项填充Combobox。 显示所有项目,并且显示的选定值在组合框中以及所有其他可能的值,但是我无法在我的DatagridViewComboBox中切换gruppe,也不允许我向Datagrid添加新行。

public Produktpflege()
    {
        InitializeComponent();
        dataGridView1.Columns[0].ValueType = typeof(int);
        dataGridView1.Columns[1].ValueType = typeof(string);
        dataGridView1.Columns[2].ValueType = typeof(String); //changed Gruppe to string @ChetanRanpariya's comment, but no change in behaviour
        dataGridView1.Columns[3].ValueType = typeof(double);
        dataGridView1.Columns[4].ValueType = typeof(double);
        dataGridView1.Columns[5].ValueType = typeof(double);
        dataGridView1.Columns[6].ValueType = typeof(double);
        dataGridView1.Columns[7].ValueType = typeof(double);
        gridColGruppe.DataSource = Core.Gruppen.Select(item=>item.Name).ToList(); //get all items from ListBinding Gruppe
    }

    private void Produktpflege_Load(object sender, EventArgs e)
    {
        foreach (var v in Core.Produkte) //create a new line foreach Produkt in Datagrid
        {
            var gdvRow = new DataGridViewRow();

            for (int i = 0; i < 8; i++) //create cells
            {
                if (i == 2) //Gruppe needs to be a combobox
                    gdvRow.Cells.Add(new DataGridViewComboBoxCell());
                else //all other are only text
                    gdvRow.Cells.Add(new DataGridViewTextBoxCell());
            }

            gdvRow.Cells[0].Value = v.ID; //invisible for user
            gdvRow.Cells[1].Value = v.Name;
            gdvRow.Cells[2].Value = v.Gruppe.Name; //string with Gruppe-Name
            gdvRow.Cells[3].Value = v.Kohlenhydrate_je_100g;
            gdvRow.Cells[4].Value = v.Eiweiss_je_100g;
            gdvRow.Cells[5].Value = v.Fett_je_100g;
            gdvRow.Cells[6].Value = v.Alkoholgehalt_Prozent;
            gdvRow.Cells[7].Value = v.KCAL; //readonly

            dataGridView1.Rows.Add(gdvRow);
        }
    }

取消组合框中Gruppe的任何更改&#34; System.FormatException:DataGridViewComboBoxCell-Value无效。&#34;以及如果我只是加载表格并填写数据。

FormDesigner:

 private void InitializeComponent()
    {
        this.components = new System.ComponentModel.Container();
        this.dataGridView1 = new System.Windows.Forms.DataGridView();
        this.gridColID = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.gridColName = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.gridColGruppe = new System.Windows.Forms.DataGridViewComboBoxColumn();
        this.gridColKohlenhydrate_je_100g = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.gridColEiweiss_je_100g = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.gridColFett_je_100g = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.gridColAlkoholgehalt_in_Prozent = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.gridColKCAL = new System.Windows.Forms.DataGridViewTextBoxColumn();
        this.gruppeBindingSource = new System.Windows.Forms.BindingSource(this.components);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
        ((System.ComponentModel.ISupportInitialize)(this.gruppeBindingSource)).BeginInit();
        this.SuspendLayout();
        // 
        // dataGridView1
        // 
        this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
        this.gridColID,
        this.gridColName,
        this.gridColGruppe,
        this.gridColKohlenhydrate_je_100g,
        this.gridColEiweiss_je_100g,
        this.gridColFett_je_100g,
        this.gridColAlkoholgehalt_in_Prozent,
        this.gridColKCAL});
        this.dataGridView1.Location = new System.Drawing.Point(12, 58);
        this.dataGridView1.Name = "dataGridView1";
        this.dataGridView1.Size = new System.Drawing.Size(983, 192);
        this.dataGridView1.TabIndex = 0;
        this.dataGridView1.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.dataGridView1_DataError);
        // 
        // gridColID
        // 
        this.gridColID.HeaderText = "ID";
        this.gridColID.Name = "gridColID";
        this.gridColID.ReadOnly = true;
        this.gridColID.Visible = false;
        // 
        // gridColName
        // 
        this.gridColName.HeaderText = "Name";
        this.gridColName.Name = "gridColName";
        // 
        // gridColGruppe
        // 
        this.gridColGruppe.DataSource = this.gruppeBindingSource;
        this.gridColGruppe.DisplayStyle = System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox;
        this.gridColGruppe.HeaderText = "Gruppe";
        this.gridColGruppe.Name = "gridColGruppe";
        this.gridColGruppe.ValueMember = "Name";
        // 
        // gridColKohlenhydrate_je_100g
        // 
        this.gridColKohlenhydrate_je_100g.HeaderText = "Kohlenhydrate_je_100g";
        this.gridColKohlenhydrate_je_100g.Name = "gridColKohlenhydrate_je_100g";
        // 
        // gridColEiweiss_je_100g
        // 
        this.gridColEiweiss_je_100g.HeaderText = "Eiweiss_je_100g";
        this.gridColEiweiss_je_100g.Name = "gridColEiweiss_je_100g";
        // 
        // gridColFett_je_100g
        // 
        this.gridColFett_je_100g.HeaderText = "Fett_je_100g";
        this.gridColFett_je_100g.Name = "gridColFett_je_100g";
        // 
        // gridColAlkoholgehalt_in_Prozent
        // 
        this.gridColAlkoholgehalt_in_Prozent.HeaderText = "Alkoholgehalt_in_Prozent";
        this.gridColAlkoholgehalt_in_Prozent.Name = "gridColAlkoholgehalt_in_Prozent";
        // 
        // gridColKCAL
        // 
        this.gridColKCAL.HeaderText = "KCAL";
        this.gridColKCAL.Name = "gridColKCAL";
        this.gridColKCAL.ReadOnly = true;
        // 
        // gruppeBindingSource
        // 
        this.gruppeBindingSource.DataSource = typeof(Kohlenhydrate_Datenbank.Gruppe);
        // 
        // Produktpflege
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(1007, 262);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.dataGridView1);
        this.Name = "Produktpflege1";
        this.Text = "Produktpflege";
        this.Load += new System.EventHandler(this.Produktpflege_Load);
        ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
        ((System.ComponentModel.ISupportInitialize)(this.gruppeBindingSource)).EndInit();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.DataGridView dataGridView1;
    private DataGridViewTextBoxColumn gridColID;
    private DataGridViewTextBoxColumn gridColName;
    private DataGridViewComboBoxColumn gridColGruppe;
    private BindingSource gruppeBindingSource;
    private DataGridViewTextBoxColumn gridColKohlenhydrate_je_100g;
    private DataGridViewTextBoxColumn gridColEiweiss_je_100g;
    private DataGridViewTextBoxColumn gridColFett_je_100g;
    private DataGridViewTextBoxColumn gridColAlkoholgehalt_in_Prozent;
    private DataGridViewTextBoxColumn gridColKCAL;

如果我能在接下来的两周内得到你的帮助,我真的很高兴。请不要告诉我使用WPF,因为我需要它在Windows Forms中运行以支持Windows XP。

0 个答案:

没有答案