The following code worked for me but I wanted to re-do it to learn how to make a Object Array. The first set of code is the working code, the 2nd is my attempt at it. Thanks for checking this out:

public void AddPrices()
        {
            string[] cb = new string[11];
            int i = 0;
            if (tabControl1.SelectedIndex == 0)
            {
                foreach (Control c in tabControl1.SelectedTab.Controls)
                {
                    if (c is ComboBox)
                    {
                        cb[i] = "[SLS-02] = '" + c.Text + "'";
                        i++;
                    }
                }
            }
            DataRow[] rcb1 = dt.Select(cb[0]);
            DataRow[] rcb2 = dt.Select(cb[1]);
            DataRow[] rcb3 = dt.Select(cb[2]);
            DataRow[] rcb4 = dt.Select(cb[3]);
            DataRow[] rcb5 = dt.Select(cb[4]);
            DataRow[] rcb6 = dt.Select(cb[5]);
            DataRow[] rcb7 = dt.Select(cb[6]);
            DataRow[] rcb8 = dt.Select(cb[7]);
            DataRow[] rcb9 = dt.Select(cb[8]);
            DataRow[] rcb10 = dt.Select(cb[9]);
            DataRow[] rcb11 = dt.Select(cb[10]);

            labelPrice.Text = string.Empty;
            decimal sumprice = 0;
            decimal[] cbp = new decimal[11];
            for (int k = 0; k <= 10; k++)
            {
                cbp[k] = 0;
            }

            foreach (DataRow row in rcb1)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[0] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[0];
            }
            foreach (DataRow row in rcb2)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[1] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[1];
            }
            foreach (DataRow row in rcb3)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[2] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[2];
            }
            foreach (DataRow row in rcb4)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[3] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[3];
            }
            foreach (DataRow row in rcb5)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[4] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[4];
            }
            foreach (DataRow row in rcb6)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[5] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[5];
            }
            foreach (DataRow row in rcb7)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[6] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[6];
            }
            foreach (DataRow row in rcb8)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[7] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[7];
            }
            foreach (DataRow row in rcb9)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[8] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[8];
            }
            foreach (DataRow row in rcb10)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[9] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[9];
            }
            foreach (DataRow row in rcb11)
            {
                labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                cbp[10] = Convert.ToDecimal(row[1].ToString());
                sumprice += cbp[10];
            }

            labelPrice.Text += "\nTotal: $" + sumprice;            
        }

My attempt (it compiles fine but when I do a testrun stacktrace points the error at "string[] cb = new string[11];":

public void AddPrices()
        {
            string[] cb = new string[11];
            for (int h = 0; h <= 10; h++)            
                cb[h] = string.Empty;

            int i = 0;
            if (tabControl1.SelectedIndex == 0)
            {
                foreach (Control c in tabControl1.SelectedTab.Controls)
                {
                    if (c is ComboBox)
                    {
                        cb[i] = "[SLS-02] = '" + c.Text + "'";
                        i++;
                    }
                }
            }

            Object[][] rcb = new object[11][];
            for (int j = 0; j <= 10; j++)
            {
                rcb[j] = new DataRow[11];
                rcb[j] = dt.Select(cb[j]);
            }

            labelPrice.Text = string.Empty;
            decimal sumprice = 0;
            decimal[] cbp = new decimal[11];
            for (int k = 0; k <= 10; k++)            
                cbp[k] = 0;            
            
            int l = 0;
            foreach (Object[] o in rcb)
            {
                foreach (DataRow[] row in rcb)
                {
                    labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                    cbp[l] = Convert.ToDecimal(row[1].ToString());
                    sumprice += cbp[l];
                    l++;
                }
            }
            
            labelPrice.Text += "\nTotal: $" + sumprice;            
        }
        }

Recommended Answers

All 10 Replies

Made another attempt, but still doesn't work:

Object[][] rcb = new object[11][];
            for (int j = 0; j <= 10; j++)
            {
                rcb[j] = new DataRow[11];
                rcb[j] = dt.Select(cb[j]);
            }           
            for (int l = 0; l <= 10; l++)
            {
                foreach (DataRow[] row in rcb[l])
                {
                    labelPrice.Text += row[0].ToString() + " " + row[1].ToString() + "\n";
                    cbp[l] = Convert.ToDecimal(row[1].ToString());
                    sumprice += cbp[l];                    
                }
            }

Why do you want this in an object array?
You really wouldn't be better off.
A better approach might be to move the data into a class and make an array or list or dictionary of the class based on how you will use it/them.

With a collection of classes, you could have a better reach into the exact element you want without so much looping and searching.

I'm not doing anything complicated, just adding the "price" column and outputing to a label. Afterwards I remove the datatable and clear the dataset so that it can refill another table. On my second attempt it looks like it would of worked but I still don't know why I get an error. Stacktrace points it at line: "foreach (DataRow[] row in rcb[l])"

1) What is the actual error you are getting?
2) What is the original source of the data? -- a database table/query?
3) Is your output a datagrid or something else?

1)
2) Original source is an excel file. I used OLEDB connection to transfer it to a datatable dt. 11 comboboxes are used to search the 11 prices.
3) Output is to a label for now called labelPrice.

I did some more research and tried out a DataRowCollection.Add method... I think that might have been a better question to have asked in the beginning. Here let me try to simplify what I want:

Is there a way I can do this in a for loop with an array? Do I use DataRowCollection.Add()? If so, then how?:

DataRow[] rcb1 = dt.Select(cb[0]);
            DataRow[] rcb2 = dt.Select(cb[1]);
            DataRow[] rcb3 = dt.Select(cb[2]);
            DataRow[] rcb4 = dt.Select(cb[3]);
            DataRow[] rcb5 = dt.Select(cb[4]);
            DataRow[] rcb6 = dt.Select(cb[5]);
            DataRow[] rcb7 = dt.Select(cb[6]);
            DataRow[] rcb8 = dt.Select(cb[7]);
            DataRow[] rcb9 = dt.Select(cb[8]);
            DataRow[] rcb10 = dt.Select(cb[9]);
            DataRow[] rcb11 = dt.Select(cb[10]);

Yes, there are many of was of filling DataRow Arrays. I'm concerned at the use of DataRow arrays, however. It seems like you're moving from one unfriendly container to another unfriendly container when an array of custom classes might do you better. ...but that depends on the query pulling the data and where it's going.

I'm not sure how to make an array of custom classes. Do I use a Collection Class? I was able to use a dictionary but now I'm planning on outputting the search results onto a datagridview and it's getting complicated converting a dictionary to a datatable..

Could I please see the SQL you're using for this?
Your code can be simplified (depending on the SQL).

I found a way to convert the dictionary to a datatable. It's working fine now, thnx anyway.

I understand.
I think there is something better you can do, though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.