如何绑定List< T>的DataGridView或BindingList< T>
问题描述:
我已经做了一千次,它的工作,但现在....不:(
I've done it one thousand of times and it works but now .... not :(
我在这里做错了,因为没有显示网格?
Am I doing something wrong here because nothing is shown in grid ?
namespace theGridIsNotWorking
{
using System;
using System.Collections.Generic;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var items = new List<Item>();
items.Add(new Item(){ TheName = "first"});
items.Add(new Item(){ TheName = "Second"});
items.Add(new Item(){ TheName = "Third"});
dataGridView1.DataSource = new List<Item>(items);
}
public class Item
{
public string TheName;
}
}
}
没有什么乐谱.... b真的很难过。
Nothing spectaculos .... but really sad.
答
我认为问题是TheName是一个成员变量,但是你需要一个属性。
尝试以下项目类:
I think the problem is that TheName is a member variable, but you need a property. Try the following for the Item class:
public class Item
{
public string TheName;
public string TheNameProperty
{
get
{
return TheName;
}
}
public Item(string name)
{
TheName = name;
}
}