DataGridViewComboBoxCell绑定 – “值无效”

我正在尝试将DataGridView中的单独ComboBox单元绑定到自定义类,并继续收到错误

DataGridViewComboBoxCell值无效

我目前正在从我所拥有的词典中将单元格的数据源分配给IList 。 但是,在设置数据源时,未设置ComboBoxCell的索引,因此选择了无效值。

我试图弄清楚如何让它选择一个真正的值,例如列表中的第0项,它已被删除此错误,或找到解决问题的另一种方法。 有人有什么建议吗?

在发布问题后不久,我设法找到了解决方案。 对于其他任何人:

问题是我试图将DataGridViewComboBoxCell.Value分配给一个对象,期望这是因为Cell绑定到一个数据源,它将自动在源中找到对象并进行更新。

实际情况并非如此,实际上您需要将值设置为等于ValueMember属性的值,以便正确更新值和绑定。 我相信我正在为ValueMemberDisplayMember使用属性’Name’(控制在单元格中呈现的方式),因此将Value设置为interface.ToString() (而不是接口实例)适用于大多数情况。 然后我捕获并忽略在我更改源时发生的任何DataErrorexception。

这是使用枚举时的简单解决方案

 ColumnType.ValueType = typeof (MyEnum); ColumnType.DataSource = Enum.GetValues(typeof (MyEnum)); 

你可以在“InitializeComponent();”之后做到这一点。

经过几个小时的试验,我终于找到了一个有效的解决方案。

 // Create a DataGridView System.Windows.Forms.DataGridView dgvCombo = new System.Windows.Forms.DataGridView(); // Create a DataGridViewComboBoxColumn System.Windows.Forms.DataGridViewComboBoxColumn colCombo = new System.Windows.Forms.DataGridViewComboBoxColumn(); // Add the DataGridViewComboBoxColumn to the DataGridView dgvCombo.Columns.Add(colCombo); // Define a data source somewhere, for instance: public enum DataEnum { One, Two, Three } // Bind the DataGridViewComboBoxColumn to the data source, for instance: colCombo.DataSource = Enum.GetNames(typeof(DataEnum)); // Create a DataGridViewRow: DataGridViewRow row = new DataGridViewRow(); // Create a DataGridViewComboBoxCell: DataGridViewComboBoxCell cellCombo = new DataGridViewComboBoxCell(); // Bind the DataGridViewComboBoxCell to the same data source as the DataGridViewComboBoxColumn: cellCombo.DataSource = Enum.GetNames(typeof(DataEnum)); // Set the Value of the DataGridViewComboBoxCell to one of the values in the data source, for instance: cellCombo.Value = "Two"; // (No need to set values for DisplayMember or ValueMember.) // Add the DataGridViewComboBoxCell to the DataGridViewRow: row.Cells.Add(cellCombo); // Add the DataGridViewRow to the DataGridView: dgvCombo.Rows.Add(row); // To avoid all the annoying error messages, handle the DataError event of the DataGridView: dgvCombo.DataError += new DataGridViewDataErrorEventHandler(dgvCombo_DataError); void dgvCombo_DataError(object sender, DataGridViewDataErrorEventArgs e) { // (No need to write anything in here) } 

就这些。

我有同样的问题。

在我的情况下,解决方案是填充外键表的数据适配器。 这不是自动填充,这是问题的原因。

Page_Load事件中:

 Me.TblUserTypesTableAdapter.Fill(Me.DonateDataSet.tblUserTypes) 

我有同样的问题。 在(unbouod)DataGrid中填充我的ComboBox列之后,我通过设置DataGridViewComboBoxColumn的ValueMember属性解决了我的问题。显然,仅仅依靠ComboBox中对象的ToString()属性是不够的。

实际代码:

 ///  /// Populate the dataGridSplitVolumes data grid with all existing qualifications for the plan. ///  ///  private void PopulateDataGridSplitVolumes(Bonus_Group bonus) { try { List qualifications = Qualification.Load(this.groupBonus.PlanID, this.ConnectionString); foreach (Qualification qual in qualifications) { DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn)this.dataGridSplitVolumes.Columns[0]; col.Items.Add(qual); } SplitVolumeGrid_QualificationColumn.ValueMember = "Name"; } catch (Exception ex) { #if DEBUG System.Diagnostics.Debugger.Break(); #endif throw ex; } }//PopulateDataGridSplitVolumes 

使用DataError事件处理程序,

 private void shahriartableDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) { //You don't have to write anything here ! } 

并且您的DisplayMember和ValueMember数据类型应该相同,在我的情况下,我有两个CountryName。 一切都适合我…… !!

为单元格设置一个空值:

 dataGridView.CurrentRow.Cells[NAME].Value = null; 

为了那些没有和我一样挣扎的人。

绑定组合时,您正在设置DisplayMember(用户将看到的内容)和ValueMember(您的应用程序将获得的内容)。

设置完成后,您需要设置值,这是失败的地方。 基本上,值的TYPE需要与ValueMember相同的TYPE。

因此,如果您的值成员显然是ID类型的ID,则需要将值设置为int,例如Cell.Value = 1;。

我遇到了同样的问题。 消息是100%现货。 combobox的值如下:Exact,StartsWith ……我试图设置值Exactă(不是Exact)。 这是在我使用DataTable.ReadXml(…)从.xml文件中读取DataGridView的DataTable时自动发生的。 .xml文件中的值已关闭。