You need two foreach loops: One to collect the values, and one to output them.
Dictionary<string, StringBuilder> dict = new Dictionary<string, StringBuilder>();
foreach (DataRow row in dt.Rows)
{
string msg = (string)row["MSG"];
string to = (string)row["TO"];
if (!dict.ContainsKey(msg))
{
dict.Add(msg, new StringBuilder(to));
}
else
{
dict[msg].AppendFormat(",{0}", to);
}
}
foreach (string key in dict.Keys)
{
Console.WriteLine("{0} {2}", key, dict[key].ToString());
}