0

I am trying to write a program that requires me to add data to a datagrid. There's a pattern, but I can't seem to figure out how to decently get this data done through a For-Loop.

This is my code without using a For-Loop:

table.Rows.Add("0", "0", "0", "0", "0");
table.Rows.Add("0", "0", "0", "1", "0");
table.Rows.Add("0", "0", "1", "0", "0");
table.Rows.Add("0", "1", "0", "0", "0");

table.Rows.Add("1", "0", "0", "0", "0");
table.Rows.Add("1", "0", "0", "1", "0");
table.Rows.Add("1", "0", "1", "0", "0");

table.Rows.Add("1", "1", "0", "0", "0");
table.Rows.Add("1", "1", "0", "1", "0");

table.Rows.Add("1", "1", "1", "0", "0");
table.Rows.Add("1", "1", "1", "1", "0");

The last zero will be dynamically generated, no need to do anything with that.

Is this somehow possible to do through a For-Loop?

3
  • from where this data is coming? loop through the actual data source. you can simply bind the table with your data source , if any. Commented Oct 23, 2012 at 11:52
  • 1
    You already have readable and working code, why change it? Commented Oct 23, 2012 at 11:55
  • Calculations will be done on the numbers. For example, the first two numbers of the row are paired and have to be calculated with a specific - and dynamically set - operator. Same for the third and fourth number. Then, the outcome of those two pairs will be calculated again and filled in on the 5th entry of the row. Search for Truthtable. Commented Oct 23, 2012 at 11:58

2 Answers 2

2

You could use this loop, although it is not very readable. So if you can use your static code, use it.

bool allFieldsOne = false;
table.Rows.Add("0", "0", "0", "0", "0");
while (!allFieldsOne)
{
    DataRow lastRow = table.Rows[table.Rows.Count - 1];
    DataRow currentRow = table.Rows.Add();
    currentRow[4] = "0";
    for (int f = 3; f >= 0; f--)
    {
        if (lastRow.Field<string>(f) == "0")
        {
            currentRow[f] = "1";
            while (--f >= 0)
                currentRow[f] = "0";
            break;
        }
        else
        {
            currentRow[f] = "1";
            allFieldsOne = f == 0;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Would that kind of for loop pattern be useful ? (it's the pattern used for truth table which is simply binary counting)

for (int i = 0; i < 16; i++)
            {
                Console.WriteLine(
                   string.Format("{0:0000}",
                   Convert.ToInt16(Convert.ToString(i, 2)))
                );
            }

/* 
OUTPUT
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
*/

2 Comments

The author's current code includes 30 in binary. This would indicate the loop should count to 30 not 16.
That would be 32 for a truth table and from the author itself : The last zero will be dynamically generated, no need to do anything with that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.