31

DataGridView 内の個別の ComboBox セルをカスタム クラスにバインドしようとしていますが、エラーが発生し続けます

DataGridViewComboBoxCell の値が無効です

IList<ICustomInterface>現在、セルのデータ ソースを、取得したディクショナリから割り当てています。ただし、データ ソースの設定時に のインデックスがComboBoxCell設定されていないため、無効な値が選択されています。

このエラーを削除するために指定されたリスト内の 0 番目の項目など、実際の値を選択する方法、または問題を解決する別の方法を見つける方法を見つけようとしています。誰にも提案はありますか?

4

10 に答える 10

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

私のように苦労しない人のために。

コンボをバインドするときは、DisplayMember (ユーザーに表示されるもの) と ValueMember (アプリケーションが取得するもの) を設定します。

これらを設定した後、値を設定する必要がありますが、ここで失敗します。基本的に、値の TYPE は ValueMember と同じ TYPE である必要があります。

したがって、値メンバーが明らかに INT 型の ID である場合、Cell.Value = 1; のように値を int に設定する必要があります。

于 2015-12-11T09:12:37.890 に答える
1

私は同じ問題を抱えています。(unboud) DataGrid に ComboBox 列を設定した後、ComboBox 内のオブジェクトDataGridViewComboBoxColumn のプロパティに依存するだけToString()では十分ではないように、ValueMember プロパティを設定することで問題を解決しました。

実際のコード:

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

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 に答える