This is a migrated thread and some comments may be shown as answers.

Programmatic creation of Grid with Update/Insert/Delete in Hierarchy using Entity Framework

1 Answer 193 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Fred Dennis
Top achievements
Rank 1
Fred Dennis asked on 23 Dec 2009, 08:16 PM
Greetings,

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

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 Dec 2009, 10:16 AM
Hi Fred   ,

1#. You need to make sure that  the primary key column name for each table in the grid source (used for master/detail table population) should be added to the DataKeyNames collection of the respective master/detail table;

#2. Try using the AdvancedDataBindingTechnique to bind the grid ie the NeedDataSource and the DetailTableBind event of the RadGrid to bind the MasterTabelView and DetailTables respectively.

You can check out some of the links below :
http://www.telerik.com/help/aspnet/grid/grdhierarchicaldatabindingusingdetailtabledatabind.html
http://www.telerik.com/help/aspnet/grid/grdhierarchicaldatabindingusingneeddatasource.html
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html

Thanks,
Shinu

Tags
Grid
Asked by
Fred Dennis
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or