31

我正在尝试将 DataGridView 中的单独 ComboBox 单元格绑定到自定义类,并不断收到错误消息

DataGridViewComboBoxCell 值无效

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

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

4

10 回答 10

36

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

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

实际上并非如此,您实际上需要将值设置为等于ValueMember属性的值,才能正确更新值和绑定。我相信我对两者都使用了属性“名称” ValueMemberDisplayMember控制单元格中的呈现方式),因此将值设置为interface.ToString()(而不是接口实例)适用于大多数情况。然后我捕获并忽略在更改源时发生的任何 DataError 异常。

于 2009-03-17T15:55:20.597 回答
18

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

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

您可以在“InitializeComponent();”之后执行此操作

于 2014-01-22T14:04:52.013 回答
5

经过数小时的试验,我终于找到了一个可行的解决方案。

// 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)
}

就这些。

于 2009-08-18T09:30:00.373 回答
2

我有同样的问题。

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

Page_Load事件中:

Me.TblUserTypesTableAdapter.Fill(Me.DonateDataSet.tblUserTypes)
于 2010-10-24T02:14:04.170 回答
1

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

实际代码:

/// <summary>
/// Populate the dataGridSplitVolumes data grid with all existing qualifications for the plan.
/// </summary>
/// <param name="bonus"></param>
private void PopulateDataGridSplitVolumes(Bonus_Group bonus)
{
  try
  {
    List<Qualification> 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     
于 2011-05-09T14:42:59.463 回答
1

为了人们不像我那样挣扎。

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

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

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

于 2015-12-11T09:12:37.890 回答
1

使用 DataError 事件处理程序,

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

并且您的 DisplayMember 和 ValueMember 数据类型应该相同,在我的情况下,我对两者都有 CountryName 。一切对我来说都很好......!

于 2016-12-20T09:38:24.237 回答
1

这是一个带有基本表单并DataGridView通过设计器添加的完整示例:

设置和绑定:

private void Form1_Load(object sender, EventArgs e)
{

    var colors = new List<Code>()
    {
        new Code() {Value= "R", Text = "Red"},
        new Code() {Value= "G", Text = "Green"},
        new Code() {Value= "B", Text = "Blue"}
    };

    var users = new List<User>()
    {
        new User() {Name = "Briana", FavoriteColor = "B"},
        new User() {Name = "Grace", FavoriteColor = "G"}
    };

    var colorCol = new DataGridViewComboBoxColumn();
    colorCol.DataSource = colors;
    colorCol.DisplayMember = "Text";
    colorCol.ValueMember = "Value";
    colorCol.DataPropertyName = "FavoriteColor";

    dataGridView1.Columns.Add(colorCol);
    dataGridView1.DataSource = users;

}

一些课程:

public class Code
{
    public string Value { get; set; }
    public string Text { get; set; }
}

public class User
{
    public string Name { get; set; }
    public string FavoriteColor { get; set; }
}
于 2019-11-14T17:56:47.027 回答
0

为单元格设置一个空值:

dataGridView.CurrentRow.Cells[NAME].Value = null;
于 2013-10-20T04:26:41.523 回答
0

我遇到了同样的问题。该消息是100%的。组合框的值类似于:Exact、StartsWith... 我试图设置值 Exactă(不是 Exact)。当我使用 DataTable.ReadXml(...) 从 .xml 文件中读取 DataGridView 的 DataTable 时,这是自动发生的。.xml 文件中的值已关闭。

于 2018-02-24T10:53:58.670 回答