• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

Binding template field in GridView control from Data coming from another field other than the foreign in other table

I have a gridview control. In the attahced  script,there is a template  in which I am Binding company name.
It works fine, but I don't  want this value. it picks the comapny Name in other table named Companies

The table companies has three important fields
ComapnyId
CompanyName
BelongstocompanyId .(  is a subset of company ids are company)

Bind("CompanyName") is getting the information fon the basis of comapnyID,  but i want that  to pick  first belongstocompanyId and on that basis  pick comapnyId, and then CompanyName
asp:GridView ID="gvEvents" AutoGenerateColumns="false" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="dataTable_Header" CellPadding="2" RowStyle-CssClass="dataTable_Item" BorderColor="#666666" runat="server">
    <Columns>
        <asp:BoundField DataField="EventName" HeaderText="Arrangementnavn" />
        <asp:BoundField DataField="EventCode" HeaderText="Arr.kode" />
         <asp:TemplateField>
           <HeaderTemplate>Kontonavn </HeaderTemplate>
         <ItemTemplate>
         <asp:Label ID="lblCompany" runat="server" Text='<%# Bind("CompanyName")%>'></asp:Label>

         
         </ItemTemplate>
         </asp:TemplateField>
 </Columns>
</asp:GridView>

Open in new window

0
Ammar Iqbal
Asked:
Ammar Iqbal
2 Solutions
 
MrunalCommented:
For this you have to update your data-source in such a way that, take column name in data-source itself (sql query, if you are using) with alias name.
And bind this alias name into your gridview column template.

sql eg.
select companyname, companyid, companyname as ParentCompanyName
from <tablename>
...
...

then update your gridview temaplate as:

<asp:Label ID="lblCompany" runat="server" Text='<%# Bind("ParentCompanyName")%>'></asp:Label>

Hope this helps you.
0
 
Ammar IqbalAuthor Commented:
In my Datasource to Table "Events". which has a foreign key companyId  ,.This company id is connecting to the companeis table. Thats  why its binding CompanyName on the basis of companyID, but I want that it should pick BelongstoComapnyId another columni n comapnies tables (but it  is a subset of companyid as well. for example if it contains 1002, then this  1002 exists as comapnyID-primary key in someother row of the same table Companeis, and after that I can get and display CompanyName in it)
0
 
CmdoProg2Commented:
I agree with mroonal, this should come from your datasource which should use a subquery or recurvise call depending on its capability.

for SQL try..
SELECT A.companyID, C1.CompanyName, A.BelongsTocompanyID
  FROM (SELECT companyID, BelongsTocompanyID 
          FROM Events, company C
         WHERE Events.companyID = C.companyID
            -- Place other criteria here
        ) A,
        company C1
 WHERE A.BelongsTocompanyID = C1.companyID

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now