'Set value in a datagridview cell programmatically

I'm new in C# and I have a problem with setting the value of a cell programmatically.

I've created a column and added it to the DataGridView:

  DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
  column.Name = "DeclaredBy";
  column.HeaderText = "DeclaredBy";
  TaskDataGridView.Columns.Add(column);

The column shows up correctly. But when I want to set a value in any cell of this column, nothing happens, the cell is still empty:

TaskDataGridView.Rows[i].Cells["DeclaredBy"].Value = "test";


Solution 1:[1]

DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
column.Name = "DeclaredBy";
column.HeaderText = "DeclaredBy";
dataGridView1.Columns.Add(column);
dataGridView1.Rows.Add("test value");
dataGridView1.Rows[0].Cells["DeclaredBy"].Value = "changed test value";

This is working. Try on newly created DataGridView, without any columns at all. If that will work, probably you made some changes on your DataGridView that disallow changing value. Ofc, when you run this, you will see only "changed test value", but this is also working, when the last line is under another button. Another possible solution is that maybe your i is not the one you want to.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 titol