We have added a ASPxGridView to a Page and bound it to a EntityDataSource. We then changed one of the Columns to a ComboBox and attached it to a second EntityDataSource. We want to use the ASPxGridLookup in the Add/Edit Form. We created a EditItemTemplate containing the GridLookup which we bound to the second DataSource on the ComboBox Colomn we created earlier. We have enable two-way data binding on the GridLookup and also enabled the NewButton(to add new Parent Values).
Everything works fine until we attempt to Add a new Parent Value. At which time we get the following exception:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
We have found many many possible solutions on the site but none seems to work.
Please let us know what we are doing wrong or if there is an alternative way to get this to work.
We are stumped.
Attached please find a sample created on the Northwind Data base which illistrats the issue.
Thanks Kindly,
Jaco Roux
ASPxGridView - Using ASPxGridLookup with the editing functionality inside EditItemTemplate causes an exception
Answers approved by DevExpress Support
Hello Jaco Roux,
Our developers have investigated this behavior and found out that this problem occurs because the ASPxGridLookup.Value's binding expression is evaluated when ASPxGridLookup is not bound to the container. This is a typical problem and you can reproduce it with standard MS controls. You can read more information about this problem here .
A possible solution to resolve this problem is to evaluate the ASPxGridLookup.Value manually:
ASPx<dx:ASPxGridLookup ID="ASPxGridLookup1" runat="server" AutoGenerateColumns="False"
DataSourceID="CategoryDataSource" KeyFieldName="CategoryID" Value='<%# GetCategoryID(Container) %>'>
C#protected int? GetCategoryID(GridViewEditItemTemplateContainer container) {
if(ASPxGridView1.IsNewRowEditing)
return null;
return Convert.ToInt32(DataBinder.Eval(container.DataItem, "CategoryID"));
}
In this case, the two-way binding mechanism does not work and you need to handle ASPxGridView.RowUpdating and ASPxGridView.RowInserting events to populate the e.NewValues["CategoryID"] field manually:
C#protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e) {
GridViewDataColumn column = ASPxGridView1.Columns["CategoryID"] as GridViewDataColumn;
e.NewValues["CategoryID"] = (ASPxGridView1.FindEditRowCellTemplateControl(column, "ASPxGridLookup1") as ASPxGridLookup).Value;
}
Attached is a modified sample.
Thanks,
Marion
@Steve,
To process your recent post more efficiently, I created a separate ticket on your behalf: T237611: ASPxGridView - How to use ASPxGridLookup inside EditItemTemplate. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.
Funny is that this works in VB.NET out of the box, simply by Value='<%# Bind("CategoryID") %>'
Hi Lukas,
Indeed, this behavior was changed in newer versions and now it works out of the box. See the How to use ASPxGridLookup inside an ASPxGridView GridViewDataComboBoxColum bound to LinqServerModeDataSource in two-way example. Also, if you face this issue during the implementation, check Why does the "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control" error message appear for a solution.
Hello Jaco Roux,
Thank you for your application. We are examining it.
Your patience is highly appreciated.
Regards,
Marion
Hello Jaco Roux,
Thank you for your patience. I have reproduced the issue, and it looks like a bug.
I have forwarded it to our developers for a detailed investigation. Please accept our apologies for any delay in responding.
Thanks,
Marion