0
votes

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";
1
Are you working on a WinForms application? - Yvder
Yes, the datagridview is in a win form. - Bartosz Czyżowski

1 Answers

0
votes
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.