alternative in .Net to building Tables/TR/TD manually?
Posted
by egrunin
on Stack Overflow
See other posts from Stack Overflow
or by egrunin
Published on 2010-06-03T21:18:44Z
Indexed on
2010/06/03
21:24 UTC
Read the original article
Hit count: 171
I've got a pile of code here that looks like this:
if (stateTax > 0)
{
tr = new TableRow();
tbl.Rows.Add(tr);
td = new TableCell();
td.CssClass = "stdLabel";
td.Text = "NY State Tax";
tr.Cells.Add(td);
td = new TableCell();
td.Text = string.Format("{0:C}", stateTax);
td.CssClass = "justRight";
tr.Cells.Add(td);
}
This is a horrible hash of data and layout, but creating a special class or control every time something like this comes up seems almost as bad.
What I usually do is write a helper function, which is concise but ugly:
if (stateTax > 0)
MakeRow(tbl, "NY State Tax", "stdLabel",
string.Format("{0:C}", stateTax), "justRight");
And now I'm thinking: haven't I done this 100 times before? Isn't there a more modern way?
Just to be explicit: it's a whole list of miscellaneous labels and values. They don't come from any particular data source, and the rules for when rows should be suppressed vary. This example has only two columns, but I have other places with more.
© Stack Overflow or respective owner