简体   繁体   中英

Looping through cells adding content

In the an application I'm working on it should be possible to dynamically add numbers to cells in a table. The user types for example nr 1, and then the number should increment by 1 in each cell (eg. cell1 = 1, cell2 = 2, cell3 = 3 and so on).

I guess I can use either a for-loop or a for-each loop, but what I need to know is how to step through the cells and adding new content to each of them?

By the way. I am using the Table Control (not "pure HTML"), and the table contains of 7 rows and two cols. I want it to loop through the cells as following:


: 1 : 2 :

: 3 : 4 :

: 5 : 6 :

and so on...

A foreach like like this should do the trick:

int yourStartInt = 6;

foreach(Control control in YourTableID.Controls)
{
    if (control is TableRow)
    {
        foreach (Control innerControl in control.Controls)
        {
            if (innerControl != null && innerControl is TableCell)
            {
                TableCell cell = innerControl as TableCell;
                cell.Text = yourStartInt.ToString();
                yourStartInt++;
            }
        }
    }
}

You must loop through your table's controls to find the rows, and then in the rows you can find the cells in similar fashion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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