I have been struggling a couple of days now with trying to create a grid programmatically with hierarchy using the entity framework and repository pattern. For example, I am loading Product and Product Details. I am retrieving my datasource using the entity framework. See below for code to get Products and Product Details:
public
IQueryable<Product> GetProducts()
{
var products = from p in db.Product.Include("ProductDetail")
select p;
return products;
}
public IQueryable<ProductDetail> FindProductDetailsByProduct(Guid productID)
{
var productDetails = from pd in db.ProductDetail.Include("Product")
select pd;
return productDetails.Where(p=> p.Product.ProductID == productID);
}
In my codebehind I copied the DefineGridStructure function from the example given in the Grid / Creating Hierarchy Programmatically example: http://demos.telerik.com/aspnet-ajax/grid/examples/programming/hierarchy/defaultcs.aspx In this example, it uses a SQLDataSource whereas I'm wanting to bind the data programmatically. I tried setting the datasource using my GetProducts() function and convert to list and assign to grid. I have two columns ProductID and ProductName in my Masterview and two columns in my Detailview. The grid loads and is populate however, when I try to expand, it does not find the relation key (ProductID) of the Masterview. ProductID is the primary key in my Product table and it is the foreign key in my ProductDetail table.
I am new to using RadGrid and I am not seeing what I am doing wrong. See actual code below and please point me in the right direction. If there is an example that uses both programmatic creation a gird hierarchy and advanced binding I would appreciate if you could forward me the link. All the samples I have reviewed use on one aspect and I am having trouble putting it all together, including the inserts, updates and deletes. My business rules are too complex to simply use an entity datasource control.
private void DefineGridStructure()
{
RadGrid RadGridProducts = new RadGrid();
RadGridProducts.ID =
"RadGridProducts";
RadGridProducts.DataSource = GetProductDataSource();
RadGridProducts.MasterTableView.DataKeyNames =
new string[] { "ProductID" };
RadGridProducts.MasterTableView.Name =
"ProductsView";
RadGridProducts.MasterTableView.CommandItemDisplay =
GridCommandItemDisplay.Top;
RadGridProducts.Width =
Unit.Percentage(98);
RadGridProducts.PageSize = 20;
RadGridProducts.AllowPaging =
true;
RadGridProducts.AllowSorting =
true;
RadGridProducts.PagerStyle.Mode =
GridPagerMode.NextPrevAndNumeric;
RadGridProducts.AutoGenerateColumns =
false;
RadGridProducts.ShowStatusBar =
true;
RadGridProducts.AllowAutomaticDeletes =
true;
RadGridProducts.AllowAutomaticInserts =
true;
RadGridProducts.AllowAutomaticUpdates =
true;
RadGridProducts.ClientSettings.ClientEvents.OnRowDblClick =
"RowDblClick";
//Register all events for grid
RadGridProducts.ItemDeleted +=
new GridDeletedEventHandler(RadGridProducts_ItemDeleted);
RadGridProducts.ItemInserted +=
new GridInsertedEventHandler(RadGridProducts_ItemInserted);
RadGridProducts.ItemUpdated +=
new GridUpdatedEventHandler(RadGridProducts_ItemUpdated);
RadGridProducts.InsertCommand +=
new GridCommandEventHandler(RadGridProducts_InsertCommand);
RadGridProducts.ItemCommand +=
new GridCommandEventHandler(RadGridProducts_ItemCommand);
RadGridProducts.PreRender +=
new EventHandler(RadGridProducts_PreRender);
RadGridProducts.NeedDataSource +=
new GridNeedDataSourceEventHandler(RadGridProducts_NeedDataSource);
RadGridProducts.DetailTableDataBind +=
new GridDetailTableDataBindEventHandler(RadGridProducts_DetailTableDataBind);
RadGridProducts.MasterTableView.PageSize =20;
RadGridProducts.MasterTableView.EditFormSettings.UserControlName =
"~/Controls/Products/ProductDetailsControl.ascx";
RadGridProducts.MasterTableView.EditFormSettings.EditFormType =
GridEditFormType.WebUserControl;
RadGridProducts.MasterTableView.EditFormSettings.EditColumn.UniqueName =
"EditCommandColumn1";
//Add columns
GridEditCommandColumn editColumn;
editColumn =
new GridEditCommandColumn();
editColumn.ButtonType =
GridButtonColumnType.LinkButton;
RadGridProducts.MasterTableView.Columns.Add(editColumn);
GridBoundColumn boundColumn;
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"ProductID";
boundColumn.HeaderText =
"Product ID";
boundColumn.UniqueName =
"ProductID";
boundColumn.Visible =
true;
RadGridProducts.MasterTableView.Columns.Add(boundColumn);
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"ProductName";
boundColumn.HeaderText =
"Product";
RadGridProducts.MasterTableView.Columns.Add(boundColumn);
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"ProductModel";
boundColumn.HeaderText =
"Model";
RadGridProducts.MasterTableView.Columns.Add(boundColumn);
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"Weight";
boundColumn.HeaderText =
"Weight";
RadGridProducts.MasterTableView.Columns.Add(boundColumn);
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"Dimensions";
boundColumn.HeaderText =
"Dimensions";
RadGridProducts.MasterTableView.Columns.Add(boundColumn);
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"SKU";
boundColumn.HeaderText =
"SKU";
RadGridProducts.MasterTableView.Columns.Add(boundColumn);
GridButtonColumn deleteColumn;
deleteColumn =
new GridButtonColumn();
deleteColumn.ButtonType =
GridButtonColumnType.LinkButton;
deleteColumn.CommandName =
"Delete";
deleteColumn.Text =
"Delete";
deleteColumn.ConfirmText =
"Are you sure you wish to Delete this item?";
RadGridProducts.MasterTableView.Columns.Add(deleteColumn);
//Detail table - (II in hierarchy level)
GridTableView tableViewProductDetails = new GridTableView(RadGridProducts);
tableViewProductDetails.DataSource = GetProductDetailsDataSource();
tableViewProductDetails.DataBind();
tableViewProductDetails.Name =
"ProductDetailsView";
tableViewProductDetails.Width =
Unit.Percentage(100);
tableViewProductDetails.DataKeyNames =
new string[] { "ProductDetailID" };
GridRelationFields relationFields = new GridRelationFields();
relationFields.MasterKeyField =
"ProductID";
relationFields.DetailKeyField =
"ProductID"; /*This line throwing error*/
tableViewProductDetails.ParentTableRelation.Add(relationFields);
RadGridProducts.MasterTableView.DetailTables.Add(tableViewProductDetails);
//Add columns
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"ProductDetailID";
boundColumn.HeaderText =
"ProductDetailID";
boundColumn.UniqueName =
"ProductDetailID";
tableViewProductDetails.Columns.Add(boundColumn);
boundColumn =
new GridBoundColumn();
boundColumn.DataField =
"MSRP";
boundColumn.HeaderText =
"MSRP";
tableViewProductDetails.Columns.Add(boundColumn);
//Add the RadGrid instance to the controls
this.PlaceHolder1.Controls.Add(RadGridProducts);
}
Regards,
Fred