Binding a collection to DevExpress Web Grid

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 23-Nov-2004 21:03:04   

Does anyone have experience with databinding a collection to a DevExpress ASPxGrid? I am setting the grid.DataSource = myEntityCollection and then trying to set the grid.DataKeyField = table.PrimaryKeyField and I get the error:

The DataKeyField property must contain a proper name of a data source column for the grid.

I then tried setting the grid.DataKeyField to another column and I don't get this error anymore, but no data is displayed in the grid.

Here is the code I'm using.


        private void Page_Load(object sender, System.EventArgs e)
        {
            if ( !Page.IsPostBack )
                this.BuildDataGrid();
        }

        private void BuildDataGrid()
        {
            this.editorGrid = new ASPxGrid();

            EntityCollection products = this.GetProducts();

            this.editorGrid.DataSource = products;
            this.editorGrid.DataMember = "Product";
            this.editorGrid.DataKeyField = "ProductID";

            this.editorGrid.AutoGenerateColumns = false;

            // Remove existing columns before creating new ones.
            this.editorGrid.RetrieveColumns(ProcessExistingColumns.Remove);

            this.editorGrid.HeaderStyle.Font.Name = "Arial";
            this.editorGrid.HeaderStyle.Font.Bold = true;
            this.editorGrid.HeaderStyle.Font.Size = new FontUnit("14px");

            this.editorGrid.ItemStyle.Font.Name = "Arial";
            this.editorGrid.ItemStyle.Font.Size = new FontUnit("12px");

            this.editorGrid.FooterStyle.Font.Name = "Arial";
            this.editorGrid.FooterStyle.Font.Size = new FontUnit("12px");

            this.editorGrid.ItemHeight = 20;
            this.editorGrid.EditItemHeight = 23;

            if ( products.Count > 0 )
            {
                Column column;

                column = this.editorGrid.Columns.ColumnByFieldName("ProductID");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "ProductID";
                }

                column = this.editorGrid.Columns.ColumnByFieldName("StyleCode");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "Style Code";
                }

                column = this.editorGrid.Columns.ColumnByFieldName("Religion");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "Religion";
                }

                column = this.editorGrid.Columns.ColumnByFieldName("Model");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "Model";
                }

                column = this.editorGrid.Columns.ColumnByFieldName("ModelYear");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "ModelYear";
                }

                column = this.editorGrid.Columns.ColumnByFieldName("Warranty");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "Warranty";
                }

                column = this.editorGrid.Columns.ColumnByFieldName("Material");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "Material";
                }

                column = this.editorGrid.Columns.ColumnByFieldName("MaterialDescription");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "MaterialDescription";
                }

                column = this.editorGrid.Columns.ColumnByFieldName("WeightLimit");
                if ( column != null )
                {
                    column.Width = 50;
                    column.HeaderText = "WeightLimit";
                }

                this.GridPanel.Controls.Add( editorGrid );
            }
        }

        private EntityCollection GetProducts()
        {
            return ServiceFactory.GetProductMaster().GetProductsByType("BottomBracket");
        }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 23-Nov-2004 21:35:42   

'Product' is that a property of the product entity? If not, leave it empty (datamember)

Frans Bouma | Lead developer LLBLGen Pro
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 23-Nov-2004 23:50:07   

Otis wrote:

'Product' is that a property of the product entity? If not, leave it empty (datamember)

"Product" is the table name for the Product entity. I also tried commenting out the DataMember property, but I still get the same error.