Devexpress绑定索引属性

时间:2010-12-12 02:51:59

标签: javascript devexpress aspxgridview

我需要将索引属性绑定到Devexpress aspxgridview控件我在运行时创建列,并且不知道如何向FieldName提及这些属性。

这是我的类,它具有正常属性('p0')和2个索引属性('p1'和'p2')。我需要将p1和p2绑定为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; 
        } 
    }
 }

绑定类对象的示例代码

 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();

1 个答案:

答案 0 :(得分:0)

我通过在TestClass类中声明了一些 public 属性来使代码工作。 p1和p2。请注意,ASPxGridView仅显示业务对象的公共属性的内容。此外,应在Page_Init方法中调用将网格绑定到DataSource的代码。希望,这有帮助。