Devexpress bind indexed property
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();
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.
精彩评论