jaczjill
jaczjill

Reputation: 334

In ASP.Net (C#), HTML Table does not have 'COLUMN' attribute, what to do?

Basically I was doing HTML table to CSV Export in C#, So needed count of all ROWS & COLUMNS

I could get the count of Rows by Table1.Rows.Count, but cannot get the count of Columns by Table1.Columns.Count.

Any Ideas how do I get the number of columns ?

    StringBuilder sb = new StringBuilder();
     for (int i = 0; i < Table1.Rows.Count; i++)
     {
      for (int k = 0; k < **Table1.Column.Count**; k++)
       {
              //adding separator
              sb.Append(Table1.Rows[i].Cells[k].Text + ',');
        }
          //appending new line
            sb.Append("\r\n");
     }

Upvotes: 3

Views: 1720

Answers (3)

Claudio Redi
Claudio Redi

Reputation: 68440

Here you have an alternative that I think is more readable

StringBuilder sb = new StringBuilder();
foreach(TableRow row in Table1.Rows)
{
    foreach (TableCell cell in row.Cells)
    {
          //adding separator
          sb.Append(cell.Text + ',');
    }
    //appending new line
    sb.Append("\r\n");
}

Upvotes: 2

Habib
Habib

Reputation: 223392

Tables.Rows[i].Cells.Count would give you the total number of cells in a row. Which would be like columns, but may change where cell(column) spanning is different.

Upvotes: 6

paul
paul

Reputation: 22011

Why can't you use Table1.Rows[i].Cells.Count?

Upvotes: 7

Related Questions