to fix
"Index was outside the bounds of the array." error, you can use return value of
Rows.Add
as below
int rowindex = dataGridView1.Rows.Add();
dataGridView1.Rows[rowindex].HeaderCell.Value = k.ToString();
But, This is not a good user case for Parallel.ForEach. you may need to use
BeginInvoke
and also do this in some other event like button click, then your datagridview loaded and you will not get exception.
private void button1_Click(object sender, EventArgs e)
{
Parallel.For(0, 5, k =>
BeginInvoke(new Action(() =>
{
dataGridView1.Columns.Add("", k.ToString());
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[index].HeaderCell.Value = k.ToString();
})));
}