No matter what I do, I cant set a cell's value using this:
row.Cells["columnName"].Value = "some text";
I am filling a DatagridView with a Datatable as its source. But with a certain column I want the user to be able to use a dropdown to change the value. So I am Hiding the column that gets generated by the datasource, then making a new comboBox column, adding in items then transferring the values from the hidden column.
private void ShotsDGV()
{
// if there is data, clear the DGV and let the data create its own columns
if (shotsData.Rows.Count > 0)
{
shotsDataGrid.Columns.Clear();
// set new column as a dropdown column
DataGridViewComboBoxColumn newSequenceColumn = new DataGridViewComboBoxColumn();
// Assign the data from the datatable to the DGV
shotsDataGrid.DataSource = shotsData;
//Hide the Auto Generated Column
//shotsDataGrid.Columns["Sequence"].Visible = false;
// Set the Header Text of the New Column
newSequenceColumn.HeaderText = "Sequence";
// Set the Name of the New Column
newSequenceColumn.Name = "newSequenceHeader";
//get the Items from the other dataTable and add all of the options for the Dropdown
for (int i = 0; i < seqData.Rows.Count; i++)
{
newSequenceColumn.Items.Add(seqData.Rows[i].ItemArray[0].ToString());
}
// add this new column to the DGV
shotsDataGrid.Columns.Add(newSequenceColumn);
//set the new column to be in the right position
shotsDataGrid.Columns["newSequenceHeader"].DisplayIndex = 2;
// copy data from original column
foreach (DataGridViewRow row in shotsDataGrid.Rows)
{
row.Cells["newSequenceHeader"].Value = row.Cells["Sequence"].Value;
Console.WriteLine(row.Cells["newSequenceHeader"].Value + " - Tada! ");
}
}
}
I get no errors, and the console write I have directly after says that the value is set... but its just not adding the value in the actual Cell in the window... It is working as a dropdown with my options added, but the cell is blank until I manually select from the drop down. I can also set ANY other cell in any OTHER column using the same method and it works fine, but this new column I have created just wont be set.
Strangely, I have copied this code from another DataGridView using the exact same technique, and that one works perfectly. I cant understand why this new column, created the exact same way wont allow me to set any values in it.
question from:
https://stackoverflow.com/questions/66062351/cell-value-wont-change-on-new-column 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…