Click here to Skip to main content
15,888,313 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an array which return data in size like 3,6,9,12,15.. (multiplication of 3)
C#
DataTable dt = new DataTable();
string GridData = ViewState["ShowItem"].ToString();
string[] FilterData = GridData.Split('^');

Above array return data like..

FilterData [0] = "Pizza";     (Item)
FilterData [1] = "2$";        (Price)
FilterData [2] = "2";         (Quantity)
FilterData [3] = "Burger";
FilterData [4] = "5$";
FilterData [5] = "1";
FilterData [6] = "Cesa";
FilterData [7] = "7$";
FilterData [8] = "3";


now I want to enter above data as column wise in database like:
HTML
Item      Price    Quantity    Total(Price*Quantity)

now I want to run a loop that add data of particular field like:
C#
for (int nIndex = 0; nIndex < FilterData.Length; nIndex++)
 {
 DataRow drow = dt.NewRow();
drow["ProductItem"] = FilterData[nIndex].ToString();   (Add all Item)
drow["Cost"] = FilterData[nIndex].ToString();          (Add all Cost)
drow["Quantity"] = FilterData[nIndex].ToString();       (Add all Quantity)
double Total= Convert.ToDouble(FilterData[nIndex].ToString()) * Convert.ToInt32(FilterData[nIndex].ToString());       (Add all cost*Quantity)
                    drow["Total"] = Total;
dt.Rows.Add(drow);
}

So can anybody help me how could I run the loop so it add data to particular column like:

HTML
Item   Price   Quantity  Total(Price*Quantity)

Pizza    2       2             4
Burger   5       1             5
Cesa     7       3             21
Posted

This looks a lot like homework, so I'll walk you through how to think about it, hopefully you can take it from there...

A "for" loop is compromised of 3 parts:

for (initialization; stop condition; increment statement)

Right now, you are incrementing your data by 1 (++), somehow, you need to increment it by 3 since you are using 3 values in one iteration of your loop. There is an operator that both increments and assigns by a value (instead of ++, its ___).

Next, inside your loop, break things out into variables:

string productName = ... //iterator position
double price = ... //iterator + 1
int quantity = ... //iterator + 2
double total = price * quantity;

And to get the right value, just think about it. The productName is the current value of the iterator, price is the iterator + 1, quantity is the iterator + 2.
 
Share this answer
 
Comments
Mas11 20-Aug-13 2:11am    
Thanks a lot Ron!
C#
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] FilterData = new string[9];
            FilterData [0] = "Pizza";
            FilterData [1] = "2$";
            FilterData [2] = "2";
            FilterData [3] = "Burger";
            FilterData [4] = "5$";
            FilterData [5] = "1";
            FilterData [6] = "Cesa";
            FilterData [7] = "7$";
            FilterData [8] = "3";
            Console.WriteLine("Item      Price    Quantity    Total(Price*Quantity)");

            for (int i = 0; i < FilterData.Length-1;i++ )
            {
                string itme = FilterData[i];
                string price = FilterData[++i];
                int quantity = Convert.ToInt32( FilterData[++i]);
                string [] rate = price.Split('$');
                int total = Convert.ToInt32(rate[0])* quantity;
                Console.WriteLine(itme + "    " + price + "      " + quantity + "     " + total);

            }
                Console.Read();
        }
    }
}
 
Share this answer
 
Comments
Mas11 20-Aug-13 2:12am    
Thanks Sandeep.
Sandeep Singh Shekhawat 20-Aug-13 2:14am    
@Mas Your most welcome.
Hi Guys I have done this with the help of one of my friend & appreciate 'Ron Bayer' guidance.
C#
for (int nIndex = 0; nIndex < FilterData.Length; nIndex += 3)
{
    DataRow drow = dt.NewRow();
    drow["ProductItem"] = FilterData[nIndex];               // Item
    var cost = FilterData[nIndex + 1];                      // Cost
    var qty = FilterData[nIndex + 2];                       // Quantity
    drow["Cost"] = cost;
    drow["Quantity"] = qty;
    double Total = 
        Convert.ToDouble(cost.Remove(cost.Length - 1)) *    // remove $
        Convert.ToInt32(qty);                               // Total
    drow["Total"] = Total;
    dt.Rows.Add(drow);
}
 
Share this answer
 

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