You have lots of answers to play with here, but nobody has addressed the fact that you may be doing lots of string concatenations here. In general, if you are going to be appending more than a couple of elements together, you should look into using a
StringBuilder
. What you would end up with (using CPallin's excellent suggestion) is:
StringBuilder sb = new StringBuilder();
int count = dt.Rows.Count - 1;
for (int i = 0; i < count; i++)
{
sb.AppendFormat("{0}\n", dt.Rows[i]["ColorName"].ToString());
}
sb.Append(dt.Rows[count]["ColorName"].ToString());
ColorNames = sb.ToString();
Another thing to consider is whether you should be using
\n
or
Environment.NewLine
. As I'm not 100% sure what your requirements are there, I'll leave it to you to investigate which is best suited to your needs.