【问题标题】:How to bind list<myClass> to DataGridView如何将 list<myClass> 绑定到 DataGridView
【发布时间】:2014-04-04 07:45:10
【问题描述】:

我正在尝试将列表绑定到 datagridview。 我这样做:

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

datagridview 有数据,图片中的情况如何,但没有显示任何内容。

你能帮帮我吗?我该如何解决这个问题? 谢谢

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

【问题讨论】:

    标签: c# list binding datagridview show


    【解决方案1】:

    之后

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

    尝试添加

    dgv_TraceItems.DataBind();
    

    【讨论】:

    • 我不能使用 DataBind... 这是一个 Windows 窗体。
    【解决方案2】:

    使用尊重的 BLF_InfoClass 属性设置每列的“DataPropertyName”属性。您必须为数据网格创建数据列。

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

    【讨论】:

    • column = new DataGridViewTextBoxColumn(); column.DataPropertyName = "NameX"; column.Name = "NameX"; dgv_TraceItems.Columns.Add(列);不工作...
    • 发布您的自定义类
    • 你班上的 TYPE 是什么?
    • 在您的问题中也发布枚举
    • 我用虚拟数据修改了答案
    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    • 2010-10-03
    • 2011-06-04
    • 2011-06-09
    • 1970-01-01
    相关资源
    最近更新 更多