integral100x
integral100x

Reputation: 342

How can I display an image in my query using LINQ?

I want to display an airline image underneath an airline name. I have stored paths in my XML file for the airlineImage. How would I go about displaying the image in my query using LINQ?

Here's my code so far:

var query = from f in XElement.Load(MapPath("flightdata2.xml")).Elements("flight")
                    select new
                    {                        
                        Airline = (string)f.Element("airline"),
                        DepartureAirportSymbol = (string)f.Element("departureAirportSymbol"),
                        DepartTime = (string)f.Element("departureTime"),
                        DestinationAirportSymbol = (string)f.Element("destinationAirportSymbol"),
                        ArrivalTime = (string)f.Element("arrivalTime"),
                        Stops = (int)f.Element("numberOfStops"),
                        Duration = (string)f.Element("duration"),
                        Cabin = (string)f.Element("class"),
                        Price = "$" + (Int32)f.Element("price")
                    };

    this.GridView1.DataSource = query;
    this.GridView1.DataBind();

Upvotes: 0

Views: 1556

Answers (2)

jlp
jlp

Reputation: 10358

Try this:

select new
{
   Duration = (string)f.Element("duration"),
   Cabin = (string)f.Element("class"),
   Price = "$" + (Int32)f.Element("price")
   ImagePath = (string)f.Element("airlineImage")
};


<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false">
   <Columns>
      <asp:ImageField DataImageUrlField="ImagePath" 
                      DataImageUrlFormatString="~/Content/Images/{0}" />
   </Columns>
</asp:GridView>

UPDATE 1. You have to replace path ~/Content/Images/{0} in GridView to where you store your images. 2. Set AutoGenerateColumns="false" so your grid will only contains colums that you have defined.

Upvotes: 1

Chad
Chad

Reputation: 1562

I think you would be best served by loading the path into your object then setting the src attribute of the image dynamically:

<img src='<%# Eval("ImageURL") %>' />

Upvotes: 1

Related Questions