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

Posted on 2011-10-18
Last Modified: 2012-08-14
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
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">
        <asp:BoundField DataField="EventName" HeaderText="Arrangementnavn" />
        <asp:BoundField DataField="EventCode" HeaderText="Arr.kode" />
           <HeaderTemplate>Kontonavn </HeaderTemplate>
         <asp:Label ID="lblCompany" runat="server" Text='<%# Bind("CompanyName")%>'></asp:Label>


Open in new window

Question by:Ammar Iqbal
    LVL 22

    Accepted Solution

    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.
    LVL 1

    Author Comment

    by:Ammar Iqbal
    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)
    LVL 12

    Assisted Solution

    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


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now