在过去的4个小时里,我一直在尝试设置单个DataGridViewComboBoxCell的值,但一无所获。我见过的最常见的解决方案是将DataGridViewComboBoxCell的.Value成员设置为其中一项,我尝试过,但它抱怨该值无效。
DataTable documentTypes = _codedValues.GetCodedValues(Database.DOCUMENT_TYPE_TABLE); documentTypes.Columns[Database.PROFESSION_ID_COLUMN].AllowDBNull = true;
documentTypes.Columns[Database.CODE].AllowDBNull = true;
this.cbxDocumentType.DisplayMember = Database.VALUE;
this.cbxDocumentType.ValueMember = Database.CODE;
this.cbxDocumentType.DataSource = documentTypes.DefaultView;
int rowId = this.dgvDocumentList.Rows.Add(doc.actualName, doc.fileName);
DataGridViewComboBoxCell obj = (DataGridViewComboBoxCell)this.dgvDocumentList.Rows[rowId].Cells[2];
obj.Value = obj.Items[0];弹出一个消息框,告诉我DataGridViewComboBoxCell视图无效后,我看到正在设置的对象的.ToString输出,即System.Data.DataRowValue。
发布于 2016-10-21 06:40:38
根据您的数据源包含的内容,您必须使用正确的强制转换来访问正确的字段。
试试这个:
DataGridViewComboBoxCell cell =
(DataGridViewComboBoxCell)this.dgvDocumentList.Rows[rowId].Cells[2];
cell.Value = ((DataRowView)cell.Items[0]).Row.ItemArray[0];这里假设Items为DataRowViews,并且ValueMember在第一个字段中。
您可以通过编写一个中间步骤来测试类型:
var item = cell.Items[0];并使用调试器查看结果类型..
https://stackoverflow.com/questions/40165324
复制相似问题