Link to home
Start Free TrialLog in
Avatar of ACE-ITFLA
ACE-ITFLAFlag for United States of America

asked on

Set Gridview ItemTemplate Label to Related Table Column Value

Developing in ASP.Net 2.0 with VS2005 and using SQLExpress on Windows XP SP2.

I may not use the correct terminology so I apologize in advance.

I've created a gridview control on a web page. One of the columns in the gridview is called ContacID. The ContactID field is an integer value which corresponds to a primary key value in my Contacts table. If I use a Label control in the ItemTemplate for the ContactID column, it displays the integer value in the gridview list on the rendered web page. What I would like is to show the ContactName field from the Contacts table in the ItemTemplate control. I don't want to use a DropDwonList control in the ItemTemplate because then I would have to set its Enabled property to False to prevent it from being updated.

I feel there must be a standard way of doing this but, being a newbie and not understanding all the terminology yet, I just can't find the answer. I'm an expert VB6 programmer but my ASP.Net understanding is coming quite slow.

Thanks
Avatar of arhame
arhame
Flag of United States of America image

What you'll do is use an Inner Join statement on your select command, and then you can set the contactID column to visible="false" and just show the ContactName column.  An example of an inner join:

Select Table1.ContactID, Table2.ContactName FROM Table1 INNER JOIN Table1 ON Table1.ContactID = Table2.ContactName

This will pull the corresponding name from table2 (wherever your contact name is stored) that is equal to the ContactID for the record you're on.  So just replace Table1 and Table2 with your real table names, and add the additional columns you want to be pulled.  The only difference on the other columns you'll want to be pulled is you'll ened to name the table AND column, so instead of just ContactID it's now table1.ContactID, etc.

Let me know if you have any questions, inner joins are little confusing at first :)
Excuse me, typo on my inner join:  

JOIN Table1 ON Table1.ContactID = Table2.ContactName

Should be

JOIN Table1 ON Table1.ContactID = Table2.ContactID
Avatar of ACE-ITFLA

ASKER

Thank you for your reply. I considered your solution but was afraid it would render my SELECT command as not updatable. Can you set the gridview's EditItemTemplate SELECT command differently from the ItemTemplate SELECT command? I'm using inline editing.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of arhame
arhame
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I know you said you're new, so if you need clarification on anything I say or help doing it just ask.  I know at the begining your knowledge usually is confined to what you've done directly, so if you have no experience doing a drop down list in edit mode I can give more detailed instructions. :)  Just let me know.
Thank you for your help. As I had stated, I wasn't sure the SELECT COMMAND with the INNER JOIN would be updateable but it is and my issue is solved!