简体   繁体   中英

Combine data from multiple sources, display in ASP.Net GridView

I am writing a currency converting module for one of our applications. I have a list of products, a list of currencies we are interested in seeing prices for, and a list of currency rates. I want the user to be able to select which currencies from the list they see in the GridView.

I also want to be able to amend my list of currencies and include a new currency, without having to make additional changes to this module. So my GridView display has to be dynamic .

Essentially I am planning on ending up with a GridView that has the following columns:

Part No - Description - USD Price - AUD Price - GBP Price

The USD price would be static as it's our base currency, the AUD and GBP are user selected and could have potentially any number of currencies listed.

I would normally use a DataSet and DataTables for this work, but I am sure there is a "better" way to do it using System.Collections.Generics.

Right now I have all of the data I need in List collections, but there does not seem to be a way to define how these collections relate to each other, or combine these collections into one so it can be bound to a GridView.

Should I be looking at something other than List to achieve this or do I need to go back to my original approach of a DataSet and DataTables.

Thanks!

******UPDATE / SOME CODE******

OK, someone asked for some code, so I will explain a little bit more about what I have setup so far.

  1. List of Products & Currencies - These come from an SQL DB via LINQ, so they can be any of the System.Collections.Generics objects, eg List, IEnumerable etc.

  2. Currency Rates - These I am pulling from the European Bank public XML file. I download the file, strip the data I need out of it and currently store that as a List object.

I could store the currency rates in the database table as well, but then I have to have some sort of background process that goes and updates the rates each day. This way the rates only get updated when someone accesses the report function (which is only going to happen occasionally). So I would rather grab the latest rates "on demand".

What I know I need to end up with is some object that has the following structure:

PartNo - Description - Base Price - Currency Price 1, Currency Price 2, Currency Price 3

Where the number of Currency Prices is undefined , as it's based on what currencies the user wants the report to display.

It's the undefined part that I am struggling with, essentially how can I create a structured object, that I don't know the complete structure of until runtime ?

Hope this makes more sense / helps!

Just thinking out loud here, but if you stored your "foreign" prices in a Dictionary or similar data structure like so:

class Product {
    public String PartNo { get; set; }
    public String Description { get; set; }
    public Decimal BasePrice { get; set; }
    public Dictionary<String, Decimal> ForeignPrices;
}

Then you could write a simple routine that would take a collection of the above objects and convert it into a DataTable that you could then bind to. Said routine would always create the following columns:

PartNo, Description, BasePrice

It would then loop through the items in the Dictionary, adding additional columns for each item. So if you had three items in ForeignPrices:

ForeignPrices.Items.Add("AUD", 10.50);
ForeignPrices.Items.Add("GBP", 6.20);
ForeignPrices.Items.Add("CAD", 5.95);

You would end up with three additional columns on your dynamically-created DataTable:

PartNo, Description, BasePrice, AUD, GBP, CAD

Of course you may want to do away with the BasePrice property and just make "USD" another item in ForeignPrices (in which case it would simply be called Prices).

HTH.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM