DataGridViewComboBoxCell绑定 - “值无效”

时间:2009-03-17 15:45:44

标签: c# datagridview datagridviewcombobox

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

  

DataGridViewComboBoxCell值无效

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

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

10 个答案:

答案 0 :(得分:31)

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

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

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

答案 1 :(得分:13)

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

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

你可以在“InitializeComponent();”

之后做到这一点

答案 2 :(得分:3)

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

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

就是这样。

答案 3 :(得分:2)

我遇到了同样的问题。

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

Page_Load活动中:

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

答案 4 :(得分:1)

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

实际代码:

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

答案 5 :(得分:1)

使用DataError事件处理程序,

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

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

答案 6 :(得分:0)

为单元格设置空值:

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

答案 7 :(得分:0)

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

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

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

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

答案 8 :(得分:0)

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

答案 9 :(得分:0)

这是一个完整的示例,其中包含基本表单,并通过设计器添加了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; }
}