Shan Coralde
Shan Coralde

Reputation: 224

For Loop and Foreach continous loop?

I am trying to place all Column Values of a DataTable as a CheckBox in a panel.

But the problem is I keep on having the same loop. I have limited the array loop depending on the Column.Count but it's still looping.

Do note that there are tabs (they are sheets from an Excel file I've imported).

Here is a visual representation of my problem.

enter image description here

I want to cut it down until F19 and end the loop.

Here is my code:


  int dynamicHeight = 0;
        int padding = 10;

        for (int i = 0; i <= dt.Columns.Count - 1; i++)
        {

            foreach (DataColumn column in dt.Columns)
            {
                    CheckBox[] chk = new CheckBox[dt.Columns.Count];
                    chk[i] = new CheckBox();
                    chk[i].Name = column.ColumnName;
                    chk[i].Text = column.ColumnName;
                    chk[i].AutoCheck = true;
                    chk[i].Bounds = new Rectangle(10, 20 + padding + dynamicHeight, 40, 22);
                    panelCol.Controls.Add(chk[i]);
                    dynamicHeight += 20;
                    panelCol.Size = new Size(120, dynamicHeight);
                    panelCol.Controls.Add(chk[i]);
                    chk[i].Location = new Point(0, dynamicHeight);
                    chk[i].Size = new Size(120, 21);
                    panelCol.BackColor = Color.White;
                    panelCol.AutoScroll = true;
                    //panelCol.AutoScrollMinSize = new Size (0, 1200);
            }
        }

Upvotes: 2

Views: 104

Answers (2)

Grant Winney
Grant Winney

Reputation: 66499

You only need to create the array once, and you only need to iterate through it once.

int dynamicHeight = 0;
int padding = 10;

CheckBox[] chk = new CheckBox[dt.Columns.Count];

for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
    var column = dt.Columns[i];
    chk[i] = new CheckBox();
    chk[i].Name = column.ColumnName;
    chk[i].Text = column.ColumnName;
    chk[i].AutoCheck = true;
    chk[i].Bounds = new Rectangle(10, 20 + padding + dynamicHeight, 40, 22);
    panelCol.Controls.Add(chk[i]);
    dynamicHeight += 20;
    panelCol.Size = new Size(120, dynamicHeight);
    panelCol.Controls.Add(chk[i]);
    chk[i].Location = new Point(0, dynamicHeight);
    chk[i].Size = new Size(120, 21);
    panelCol.BackColor = Color.White;
    panelCol.AutoScroll = true;
    //panelCol.AutoScrollMinSize = new Size (0, 1200);
}

Actually, I don't think you even need the array, unless you're doing something with it later. You could probably remove it and just replace every instance of chk[i] with checkBox.

Upvotes: 2

PSK
PSK

Reputation: 17953

You code for adding check boxes is running dt.Columns.Count * dt.Columns.Count times, ideally it should run only dt.Columns.Count times to get the desired output.

Without changing much of your implemented code, following should work in your case.

 CheckBox[] chk = new CheckBox[dt.Columns.Count];
 for (int i = 0; i <= dt.Columns.Count - 1; i++)
            {

                chk[i] = new CheckBox();
                chk[i].Name = dt.Columns[i].ColumnName;
                chk[i].Text = dt.Columns[i].ColumnName;
                chk[i].AutoCheck = true;
                chk[i].Bounds = new Rectangle(10, 20 + padding + dynamicHeight, 40, 22);
                panelCol.Controls.Add(chk[i]);
                dynamicHeight += 20;
                panelCol.Size = new Size(120, dynamicHeight);
                panelCol.Controls.Add(chk[i]);
                chk[i].Location = new Point(0, dynamicHeight);
                chk[i].Size = new Size(120, 21);
                panelCol.BackColor = Color.White;
                panelCol.AutoScroll = true;
            }

OR

int i = 0;
CheckBox[] chk = new CheckBox[dt.Columns.Count];
foreach (DataColumn column in dt.Columns)
{

    chk[i] = new CheckBox();
    chk[i].Name = column.ColumnName;
    chk[i].Text = column.ColumnName;
    chk[i].AutoCheck = true;
    chk[i].Bounds = new Rectangle(10, 20 + padding + dynamicHeight, 40, 22);
    panelCol.Controls.Add(chk[i]);
    dynamicHeight += 20;
    panelCol.Size = new Size(120, dynamicHeight);
    panelCol.Controls.Add(chk[i]);
    chk[i].Location = new Point(0, dynamicHeight);
    chk[i].Size = new Size(120, 21);
    panelCol.BackColor = Color.White;
    panelCol.AutoScroll = true;

    i++;
    //panelCol.AutoScrollMinSize = new Size (0, 1200);
}

Upvotes: 0

Related Questions