I want to split and transpose a multiline string from one DataGridView(dgvObs) to separate rows and columns in another DataGridView(dgvSm).
This code works, but – maybe there is a better solution:
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);
}
}
Shorter you say? Try this:
COMMENT EDIT:
This will read the multiline, split it via regular expressions and then do what my original code did. Simply add the
dgvSm.Rows.Add(s);line into the nested foreach statement.