从单元格中单击时,Datagridviewcomboboxcell不会保留选定的值

时间:2019-05-29 13:01:43

标签: c# winforms datagridview

DataGridViewComboBoxColumn Description = new DataGridViewComboBoxColumn();
Description.Name = "Value";
Description.DataSource = comboBoxValues;
DataGridViewTextBoxColumn Start = new DataGridViewTextBoxColumn();
Start.Name = "Second Value";
dataGridView2.Columns.AddRange(new DataGridViewColumn[] { Start, Description });
DataTable dt = "table of values";
DataTable BindingTable = new DataTable();
for (int rowIndex = 0; rowIndex < dt.Rows.Count; rowIndex++)
    {
     DataRow newRow = dt.Rows[rowIndex];
     DataGridViewRow row = new DataGridViewRow();
     DataGridViewComboBoxCell newComboCell = new DataGridViewComboBoxCell();
     newComboCell.DisplayMember = leftColumn;
     newComboCell.ValueMember = rightColumn;
     newComboCell.ValueType = typeof(string);
     newComboCell.Value = newRow["Value"].ToString();
     newComboCell.DataSource = comboBoxValues;
     DataGridViewTextBoxCell newTextCell = new DataGridViewTextBoxCell();
     newTextCell.Value = newRow["SecondValue"];
     row.Cells.Add(newTextCell);
     row.Cells.Add(newComboCell);
     dataGridView2.Rows.Add(row);
     }
dataGridView2.Refresh();

以上是我生成datagridview的方式,并且一切正常。由于某种原因,当我单击datagridviewcombobox单元格并更改选定的值时,然后单击该单元格之外的框,然后组合框将返回到第0个索引。

0 个答案:

没有答案