Reputation: 334
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
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
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