我需要将索引属性绑定到 Devexpress aspxgridview 控件我在运行时创建列并且不知道如何将这些属性提到 FieldName。
这是我的类,它有一个普通属性('p0')和 2 个索引属性('p1' & 'p2')。我需要将 p1 和 p2 绑定为数据网格中的列。
namespace TestClass{
public class TestClass {
private int _p0;
private int _p1;
private string _p2;
public int p0 { get { return _p0; } set { _p0 = value; } }
public object this[string Field] {
get { switch (Field) {
case "p0": return _p0;
case "p1": return _p1;
case "p2": return _p2;
default: throw new IndexOutOfRangeException();
}
}
set {
switch (Field) {
case "p0": _p0 = (int)value;
break;
case "p1": _p1 = (int)value;
break;
case "p2": _p2 = (string)value;
break;
default:
throw new IndexOutOfRangeException();
}
}
}
public static TestClass[] GetABunch() {
TestClass[] result = new TestClass[1000];
for (int i = 0; i < result.Length; i++) {
TestClass x = new TestClass();
x["p0"] = i;
x["p1"] = i;
x["p2"] = "row " + i.ToString();
result[i] = x;
} return result;
}
}
}
绑定类对象的示例代码
TestClass.TestClass [] cls = TestClass.TestClass.GetABunch();
// This works since its a normal property.
GridViewDataTextColumn txtCol = new GridViewDataTextColumn();
txtCol.FieldName = "p0";
grid.Columns.Add(txtCol); // Trying to bind the indexed property, not sure how to this.
GridViewDataTextColumn txtCol1 = new GridViewDataTextColumn();
txtCol1.FieldName = "p1"; // should be something like MyObject["p1"] ?
grid.Columns.Add(txtCol1);
grid.KeyFieldName = "p0";
grid.DataSource = cls;
grid.DataBind();