Devexpress 绑定索引属性

发布于 2024-10-07 04:46:37 字数 2081 浏览 3 评论 0原文

我需要将索引属性绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-10-14 04:46:37

我通过在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文