我想要一个DataGridView到自定义类内结合单独的组合框的细胞,并保持收到错误

  

的DataGridViewComboBoxCell值是无效的

我目前分配用于小区从字典我有一个IList<ICustomInterface>的数据源。一旦设置然而数据源,用于ComboBoxCell索引没有被设置,所以它有选择的无效值。

我想弄清楚如何让它选择一个真正的价值,例如在列表中的第0项已经给出消除这种误差,或者找到另一种方式来解决这个问题。任何人有什么建议?

有帮助吗?

解决方案

我设法找到解决方案张贴问题后不长。其他任何人:

的问题是,我试图在DataGridViewComboBoxCell.Value分配到一个对象,期望是,由于细胞被绑定到数据源,它将自动查找在源和更新的对象。

这是不实际的情况下,你确实需要设置等于ValueMember财产的价值为它正确地更新值和绑定。我相信我是用属性“名称”两个ValueMemberDisplayMember(控制如何在细胞中呈现),所以其值设置为interface.ToString()(而不是接口实例),适用于大多数情况下。然后我赶上并忽略而我周围改变源发生的任何DataError异常。

其他提示

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

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

可以做到这一点刚过 “的InitializeComponent();”

Afters小时试验,终于发现,有效的解决方案。

// 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中填充我的组合框柱之后,我通过设置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     

使用DataError事件处理程序中,

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

和您的DisplayMember和ValueMember数据类型应该是相同的,在我来说,我有国家或地区名称两种。一切工作正常,我... !!

设置空值到单元:

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

对于像我一样的人不挣扎了。缘故

在结合你设置DisplayMember组合(什么,用户将看到) 和ValueMember(你的申请将获得)。

设置这些你需要设置的价值,这是它失败之后。 基本值的类型需要是相同的类型ValueMember。

因此,如果您的值构件是一个ID显然其INT类型和需要设定值为int例如Cell.Value = 1;

我有同样的问题。该消息为100%点上。对于组合框的值都像:精确,StartsWith ......我试图设置正序连赢(不准确)的值。这是自动发生的事情,因为我是从DataTable.ReadXml(...)的.xml文件阅读DataGridView中的数据表。在.xml文件的值是关断。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top