Devexpress 绑定索引属性
我需要将索引属性绑定到 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();
I need to bind indexed property to the Devexpress aspxgridview control I'm creating columns at runtime and don't know how to mention those properties to the FieldName.
Here is my class which has.a normal property ('p0') and 2 indexed properties ('p1' & 'p2'). I need to bind p1 and p2 as column in the datagrid.
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;
}
}
}
Sample Code which binds the class object
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过在 TestClass 类中声明几个 public 属性来使您的代码正常工作。 p1 和 p2。请注意,ASPxGridView 仅显示业务对象的公共属性的内容。此外,应在 Page_Init 方法中调用将网格绑定到数据源的代码。希望这有帮助。
I've made your code work by declaring a couple of public properties in the TestClass class. p1 and p2. Please note, the ASPxGridView shows content of only public properties of a business object. Also, the code to bind the grid to the DataSource should be called within the Page_Init method. Hope, this helps.