winForms + DataGridView绑定到List< T>
我试图将一个 List< T>
绑定到一个DataGridView控件,我没有任何运气创建自定义绑定。
I'm trying to bind a List<T>
to a DataGridView control, and I'm not having any luck creating custom bindings.
我试过:
gvProgramCode.DataBindings.Add(new Binding("Opcode",code,"Opcode"));
它引发异常,表示该属性名称没有找到。
It throws an exception, saying that nothing was found by that property name.
有问题的列的名称是操作码。 列表< T>
中的属性名称是操作码。
The name of the column in question is "Opcode". The name of the property in the List<T>
is Opcode.
ANSWER EDIT :问题是我没有我的类中的可绑定字段作为属性,只是公共字段...显然它不反映在字段,只是属性。
ANSWER EDIT: the problem was that I did not have the bindable fields in my class as properties, just public fields...Apparently it doesn't reflect on fields, just properties.
网格上的属性是否与Opcode绑定?如果要直接绑定到列表,您只需要DataSource = list。数据绑定允许自定义绑定。你试图做一些除数据源以外的事情吗?
Is the property on the grid you are binding to Opcode as well?.. if you want to bind directly to List you would just DataSource = list. The databindings allows custom binding. are you trying to do something other than the datasource?
你正在得到一堆空行?自动生成的列有名称吗?你验证的数据是否在对象中(不只是string.empty)?
You are getting a bunch of empty rows? do the auto generated columns have names? Have you verified data is in the object (not just string.empty) ?
class MyObject
{
public string Something { get; set; }
public string Text { get; set; }
public string Other { get; set; }
}
public Form1()
{
InitializeComponent();
List<MyObject> myList = new List<MyObject>();
for (int i = 0; i < 200; i++)
{
string num = i.ToString();
myList.Add(new MyObject { Something = "Something " + num , Text = "Some Row " + num , Other = "Other " + num });
}
dataGridView1.DataSource = myList;
}
这应该可以正常...
this should work fine...