Ticket DQ14508
Visible to All Users

Foreign keys in grids

created 19 years ago

Let's say that I am using an XtraGrid control to show values from a table (e.g. Users). However, let's say that, in that table, I am storing a foreign key that points to a record in another table (e.g. fkUserTypeID which points to pkUserTypeID in UserTypes table). I don't want to show the fkUserTypeID value in the grid…I want to show the UserTypeDesc field, from the UserTypes table, that corresponds with the stored fkUserTypeID. I want to be able to do one of two things. One would be to just show the other field. The other is that I would like to be able to show a combo box which shows. let's say, the UserTypeDesc values and let the user select one of those, but behind the scenes, the newly selected ID would be stored with the record. How would I do these things?

Comments (3)
DevExpress Support Team 19 years ago

    Hi,
    You should create the RepositoryItemLookupEdit in the grid, set its DataSource, DisplayMember, ValueMember property values and finally set the column's ColumnEdit property to this item. The second situation is covered by this answer (see the attached sample project). To implement the first, you should also handle the ShowingEditor event of the GridView and set the e.Allow parameter to False in case the View.FocusedColumn equals required column.
    Thanks,
    Plato

      OK, so I use a RepositoryItemLookupEdit…got it. However, here's another question…
      Given the tables I mentioned before, how would I deal with the fact that some UserTypes may be inactive (i.e. their IsActive fields are set to False). When this is true, you aren't supposed to be able to assign that UserType to Users anymore. However, it is possible that Users were assigned to it before and they can stay. So, in this case, the lookup needs to show only the UserTypes that are Active, but the grid cell needs to show the value assigned to the user, even if it is an Inactive one.
      How would I implement something like that?

      DevExpress Support Team 19 years ago

        Hi,
        You may use the repositoryItem.Validating event to prevent the end-user from saving a wrong (in your case, inactive) value. Please refer to the "Validating Editors" topic from the XtraGrid's documentation.
        Thanks,
        Plato

        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.