简体   繁体   English

如何绑定列表 <myClass> 到DataGridView

[英]How to bind list<myClass> to DataGridView

i´m trying to bind a list to a datagridview. 我正在尝试将列表绑定到datagridview。 i do that: 我这样做:

public void seedatagrid(List<myClass> liste2)
{
    dgv_TraceItems.DataSource = new BindingList<myClass>(liste2.ToList());
}

and the datagridview has the data, how is in the picture, but it doesn´t show anything. datagridview包含数据,图片中的数据如何,但未显示任何内容。

could you help me?? 你可以帮帮我吗?? how can i resolve the problem?? 我该如何解决这个问题? thank you 谢谢

在此处输入图片说明

public enum TYPE
{
    normal= 1,
    especial= 3, 
    low= 6, 
    high= 7,          
}


public class myClass : INotifyPropertyChanged
{

    private byte number;
    private TYPE type;
    private string file;
    private bool isselected;

    public event PropertyChangedEventHandler PropertyChanged;

    public byte Number
    {
        get
        {
            return this.number;
        }
        set
        {
            this.number= value;
            this.OnPropertyChanged("Number");
        }
    }

    public TYPE Type
    {
        get
        {
            return this.type;
        }
        set
        {
            this.type = value;
            this.OnPropertyChanged("Type");
        }
    }

    public string File
    {
        get
        {
            return this.file;
        }
        set
        {
            this.file = value;
            this.OnPropertyChanged("File");
        }
    }

    public bool IsSelected
    {
        get
        {
            return this.isselected;
        }
        set
        {
            this.isselected = value;
            this.OnPropertyChanged("IsSelected");
        }
    }

    public myClass(UInt32 Data, string Text)
    {            
        this.number = (byte)((Data & 0x0000FF00) >> 8);
        this.type = (TYPE)((Data & 0x00FF0000) >> 16);
        this.file = Text;

    }

    private void OnPropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

After

dgv_TraceItems.DataSource = new BindingList<BLF_InfoClass>(liste2.ToList());

try to add 尝试添加

dgv_TraceItems.DataBind();

set 'DataPropertyName' property of each column with the respected BLF_InfoClass properties.You have to create Data Columns for your datagrid. 使用受尊重的BLF_InfoClass属性设置每个列的'DataPropertyName'属性。您必须为datagrid创建数据列。

        dgv_TraceItems.AutoGenerateColumns = false;
        dgv_TraceItems.Columns.Add(CreateComboBoxWithEnums());

        // Initialize and add a text box column.
        DataGridViewColumn column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "number";
        column.Name = "number";
        dataGridView1.Columns.Add(column);

        // Initialize and add a text box column.
        column = new DataGridViewTextBoxColumn();
        column.DataPropertyName = "file";
        column.Name = "file";
        dataGridView1.Columns.Add(column);


        // Initialize and add a check box column.
        column = new DataGridViewCheckBoxColumn();
        column.DataPropertyName = "isselected";
        column.Name = "isselected";
        dataGridView1.Columns.Add(column);

        // Initialize the form. 
        this.Controls.Add(dataGridView1);
        this.AutoSize = true;
        List<myClass> bindingData = new List<myClass>();
        for (int i = 0; i < 5; i++)
        {
            myClass testObj = new myClass();
            testObj.File = "test" + i;
            testObj.IsSelected = true;
            testObj.Type = TYPE.high;
            bindingData.Add(testObj);
        }

        dgv_TraceItems.DataSource = bindingData;
    }

    DataGridViewComboBoxColumn CreateComboBoxWithEnums()
    {
    DataGridViewComboBoxColumn combo = new DataGridViewComboBoxColumn();
    combo.DataSource = Enum.GetValues(typeof(TYPE));
    combo.DataPropertyName = "Type";
    combo.Name = "Type";
    return combo;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM