c# winforms
I have to following string:
1,07,19,19,07
2,19,07,07,19
3,08,20,08,20
I need each line to place into separate DataGridViewRow, and each part of line (split by comma) into separate DataGridViewColumn:
1 07 19 19 07
2 19 07 07 19
3 08 20 08 20
Please, help.


VulpesRank #55 · 29.2K reputationPosted Jun 18, 2012, 11:23 AM EDT
If instead you have an array of 3 strings, 'lines', then just ignore the first line of code:
VulpesRank #55 · 29.2K reputationPosted Jun 18, 2012, 10:47 AM EDT
Memento104 reputationPosted Jun 18, 2012, 11:51 AM EDT
I'm just interesting - is here possible to write foreach line loop ?
If it is - please give me just a first line or two.
Because, next time maybe starting string could have more lines.
And congratulations for new forum design, especially for the "accepted" tape. Very nice.
VulpesRank #55 · 29.2K reputationPosted Jun 18, 2012, 12:07 PM EDT
Memento104 reputationPosted Jun 18, 2012, 12:26 PM EDT
I found that these code also works. Could you, please give me your opinion:
object val = dgvObs.Rows[0].Cells[2].Value;
if (val == null) return;
string text = val.ToString();
using (StringReader sr = new StringReader(text))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] divLine = line.Split(',');
dgvSm.Rows.Add(divLine);
}
}
VulpesRank #55 · 29.2K reputationPosted Jun 18, 2012, 12:56 PM EDT
I'd forgotten that the Rows.Add method has an overload which allows you to populate all cells from an array so that's a nice touch too :)