2014-04-04 28 views
0

我试图将列表绑定到datagridview。 我这样做:如何将列表<myClass>绑定到DataGridView

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

,并在DataGridView有数据,如何在图片,但它doesn't显示任何东西。

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

enter image description here

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

回答

0

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

尝试添加

dgv_TraceItems.DataBind(); 
+0

我不能使用DataBind ...这是一个Windows窗体。 – user3345868

+0

http://stackoverflow.com/questions/1228539/how-to-bind-list-to-datagridview – Stuntman

0

组与尊重BLF_InfoClass properties.You每列的 '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; 
    } 
+0

我该怎么做? – user3345868

+0

column = new DataGridViewTextBoxColumn(); column.DataPropertyName =“NameX”; column.Name =“NameX”; dgv_TraceItems.Columns.Add(column);没有工作... – user3345868

+0

发布您的CustomClass – Sajeetharan