1

I'm getting this error message ("No row can be added to a DataGridView control that does not have columns. Columns must be added first.") but I can't see anything wrong with my code. I swear I've done this hundreds of times but something is wrong.

class ViewItem {
   public string Name;
   public string Value;
}

...
BindingList<ViewItem> list=  new BindingList<ViewItem>();
dataGridView.DataSource = list;
ViewItem vi = new ViewItem(){Name = "Foo", Value = "Bar"};
list.Add(vi);
/// error here !
4

1 回答 1

6

这些列需要声明为属性,而不仅仅是数据成员。添加{get;set;}到要在数据网格中显示为列的班级成员。

class ViewItem {
   public string Name { get;set;}
   public string Value { get;set;}
}

...
BindingList<ViewItem> list=  new BindingList<ViewItem>();
dataGridView.DataSource = list;
ViewItem vi = new ViewItem(){Name = "Foo", Value = "Bar"};
list.Add(vi);
/// works!
于 2013-06-18T21:32:47.750 回答