I did have another question in which I thought I was well on my way only to realize that the Dictionary is of great use, but I need to be able to really use my Datatable to loop through.
The Dictionary is correctly showing the data, but my loop over the edited datatable is clearly showing both the edited and old data
https://dotnetfiddle.net/6BzsYh
DataTable table1 = new DataTable();
table1.Columns.Add("ID", typeof (int));
table1.Columns.Add("Weight", typeof (int));
table1.Columns.Add("Name", typeof (string));
table1.Columns.Add("Breed", typeof (string));
table1.Rows.Add(23, 57, "Koko", string.Empty);
table1.Rows.Add(44, 130, "Fido", null);
table1.Rows.Add(54, 130, "Jack", null);
table1.Rows.Add(44, 130, "Thad", null);
table1.Rows.Add(64, 130, "John", null);
table1.Rows.Add(23, 130, "Brian", null);
table1.Rows.Add(445, 130, "James", null);
table1.Rows.Add(64, 134, "Adam", null);
Dictionary<int, string> dict = new Dictionary<int, string>();
for (int i = 0; i < table1.Rows.Count; i++)
{
int id = Convert.ToInt32(table1.Rows[i][0]);
if (dict.ContainsKey(id))
{
//comma separate the Names
dict[id] += ", " + table1.Rows[i][2].ToString();
// change the Name value in the table
table1.Rows[i][2] = dict[id];
//Console.WriteLine(dict[id]);
}
else
{
dict.Add(id, table1.Rows[i][2].ToString());
}
//Console.WriteLine()
}
foreach (DataRow eaRow in table1.Rows)
{
Console.WriteLine("ID=" + eaRow[0] + " Name=" + eaRow[2]);
}
Console.WriteLine("\n\n");
//dictionary is correct
foreach (var item in dict)
{
Console.WriteLine("ID=" + item.Key + " Name=" + item.Value);
}