Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
100 views
in Technique[技术] by (71.8m points)

c# - Cell value wont change on new Column

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...