简体   繁体   English

计算数据表中有多少项

[英]Count how many items in a DataTable

I have this form where textboxes holds a client's education information. 我有这种表格,其中的文本框包含客户的教育信息。 I've put these textboxes in array so I can individually call them and put information to them from my datatable. 我将这些文本框放入数组中,以便可以分别调用它们,并从数据表中将信息放入它们中。 I achieve this using a for loop and counter for it is the amount of rows in the datatable. 我使用for循环和计数器来实现这一点,因为它是数据表中的行数。 I was wondering if there's a way to count the amount of items in it so I can change the counter to a much larger value and avoid all these other counters: 我想知道是否有一种方法可以计算其中的物品数量,因此我可以将计数器更改为更大的值并避免使用所有其他计数器:

        TextBox[] arrayE = {
            txtSchoolN1, txtDegree1, txtStartD1, txtEndD1,
            txtSchoolN2, txtDegree2, txtStartD2, txtEndD2,
            txtSchoolN3, txtDegree3, txtStartD3, txtEndD3,
            txtSchoolN4, txtDegree4, txtStartD4, txtEndD4,
            txtSchoolN5, txtDegree5, txtStartD5, txtEndD5,
            txtSchoolN6, txtDegree6, txtStartD6, txtEndD6,
        };
        int[] counter = { 0, 1, 2, 3 };
        for (int x = 0; x < dataTable.Rows.Count; x++)
        {
            arrayE[counter[0]].Text = dataTable.Rows[x][0].ToString().Trim();
            arrayE[counter[1]].Text = dataTable.Rows[x][1].ToString().Trim();
            arrayE[counter[2]].Text = dataTable.Rows[x][2].ToString().Trim();
            arrayE[counter[3]].Text = dataTable.Rows[x][3].ToString().Trim();
            counter[0] += 4;
            counter[1] += 4;
            counter[2] += 4;
            counter[3] += 4;
        }

In a nutshell, I'm trying to shorten my code 简而言之,我正在尝试缩短代码

Is this short enough? 这够短吗?

int countOne = 0;
for (int x = 0; x < dataTable.Rows.Count; x++)
{
    for (int y = 0; y <= 3; y++)
    {
        arrayE[countOne + y].Text = dataTable.Rows[x][y].ToString().Trim();
    }
    countOne += 4;
}

Here's one way. 这是一种方法。

var arrayE = new[]
{
    new[] {txtSchoolN1, txtDegree1, txtStartD1, txtEndD1},
    new[] {txtSchoolN2, txtDegree2, txtStartD2, txtEndD2},
    new[] {txtSchoolN3, txtDegree3, txtStartD3, txtEndD3},
    new[] {txtSchoolN4, txtDegree4, txtStartD4, txtEndD4},
    new[] {txtSchoolN5, txtDegree5, txtStartD5, txtEndD5},
    new[] {txtSchoolN6, txtDegree6, txtStartD6, txtEndD6}
};
for (var x = 0; x < dataTable.Rows.Count; x++)
{
    arrayE[x][0].Text = dataTable.Rows[x][0].ToString().Trim();
    arrayE[x][1].Text = dataTable.Rows[x][1].ToString().Trim();
    arrayE[x][2].Text = dataTable.Rows[x][2].ToString().Trim();
    arrayE[x][3].Text = dataTable.Rows[x][3].ToString().Trim();
}

Use a second, nested loop: 使用第二个嵌套循环:

int[] counter = { 0, 1, 2, 3 };
for (int x = 0; x < dataTable.Rows.Count; x++) {
    for (int i = 0; i < counter.Length; i++) {
        arrayE[counter[i]].Text = dataTable.Rows[x][i].ToString().Trim();
        counter[i] += 4;
    }
}

Or, without the counter array: 或者,没有计数器数组:

for (int x = 0; x < dataTable.Rows.Count; x++) {
    for (int y = 0; y < 4; i++) {
        arrayE[4 * x + y].Text = dataTable.Rows[x][y].ToString().Trim();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM