Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this way, the data comes from the database in a mixed way with different prices.

1. Product = Laptop Price=100 Quantity=105
2. Product = Monitor Price=110 Quantity=130
3. Product = Tel Price=200 Quantity=250
4. Product= TV Price=200 Quantity=250
5. Product= Monitor Price=140 Quantity=70
6. Product= TV Price=350 Quantity=50
7. Product= TV Price=500 Quantity=20
8. Product= Laptop Price=150 Quantity=50

I have data I retrieved using mongodb.
This data is filtered by date and returns the price/product ratio in the selected date range.
A product can be displayed at one price or at multiple prices.

In the datatable I created, only the product name is statically available.
There will be as many columns and rows as the content of the data coming from the database.
How can I prepare this?

I can do this manually in a static way.

But when the date filter expands, I won't know how many of the incoming data are and I can't prepare the table accordingly.


| Product | Price1 |Quantity1| Price2 | Quantity2 | PriceN | QuantityN |
-----------|---------|-------- |--------|-------- |----- |-------- |
| Laptop | 100 | 105 | 150 | 50 | | |
| Monitor | 110 | 130 | 140 | 70 | | |
| Tel | 200 | 250 | | | | |
| TV | 200 | 250 | 350 | 50 | 500 | 20 |



A table like this will appear.
But this table will be determined according to the data from the database.
I couldn't manage to create it dynamically...

--

What I have tried:

if this item exists in the table this way i get which row it is in



DataColumn[] colums = dt.Columns.Cast<datacolumn>().ToArray();
listed= dt.AsEnumerable()
.Any(row => colums.Any(col => row[col].ToString() == product));
if(listed==true){DataRow drow = dt.Select(("ProductName=\'" + (product+ "\'")))[0];
int tempIndex = dt.Rows.IndexOf(drow);}


it all starts here..
I don't know how many prices for this product have been entered.
Maybe the first price..
maybe third price.

It will create a new column whatever information was entered last and add the new information there.
Posted
Updated 2-Sep-22 2:05am
Comments
Richard MacCutchan 2-Sep-22 7:50am    
You first need to analyse all the incoming records to find the maximum number of items. The datatable can then be created with that number of columns, and the rows populated by the records. Any record with fewer entries will have some blank cells.
khraLiezx 2-Sep-22 8:44am    
This process will be a long process for the program..Because the number of data is determined by the date.If a long date range is selected, tens of thousands of data will come and it will take a long time for this process to happen..Thank you for your suggestion
Richard MacCutchan 2-Sep-22 9:05am    
If you need to list that many items then there is something wrong with your design.
khraLiezx 2-Sep-22 11:54am    
Why?
Richard MacCutchan 2-Sep-22 12:09pm    
How many users do you think are going to want to scroll through tens of thousands of data?

1 solution

Richard's idea is a good one and simple to implement. However it does not cater for growing data.

I suggest that you extract the data as individual rows (per product per price) and then pivot that datatable to produce another

Here are a couple of CP articles that show you how to do that
Pivoting DataTable Simplified[^]
C# Pivot Table[^]
 
Share this answer
 
Comments
Richard MacCutchan 2-Sep-22 8:33am    
+5. You can tell I'm not a database expert.
khraLiezx 2-Sep-22 8:45am    
can't I do this with normal datatable?Everything works very nicely when I add the columns myself.But adding columns manually is not healthy.A product can have 7 different prices and 7 different sales quantities.I added 5 manually.and the last 2 data cannot be accessed as healthy.
CHill60 2-Sep-22 12:17pm    
These *are* normal datatables
You could even do this as you extract the data - see Won Bachtiar's answer at https://stackoverflow.com/questions/17400575/pivot-rows-to-columns-in-mongodb[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900