Ticket B202456
Visible to All Users

ASPxGridView - Using ASPxGridLookup with the editing functionality inside EditItemTemplate causes an exception

created 14 years ago

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

Comments (2)
DevExpress Support Team 14 years ago

    Hello Jaco Roux,
    Thank you for your application. We are examining it.
    Your patience is highly appreciated.
    Regards,
    Marion

    DevExpress Support Team 14 years ago

      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

      Answers approved by DevExpress Support

      created 14 years ago (modified 12 years ago)

      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

        Show previous comments (1)
        DevExpress Support Team 10 years ago

          @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.

          LT LT
          Lukáš Trávníček 8 years ago

            Funny is that this works in VB.NET out of the box, simply by Value='<%# Bind("CategoryID") %>'

            DevExpress Support Team 8 years ago

              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.

              Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

              Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.