[英]Adding table rows through For-Loop
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. 有一种模式,但我似乎无法弄清楚如何通过For-Loop正确地获得这些数据。
This is my code without using a For-Loop: 这是我的代码,没有使用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? 这是否可以通过For-Loop进行?
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;
}
}
}
Would that kind of for loop pattern be useful ? 那种for循环模式会有用吗? (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
*/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.