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");
}