2

私は3つの列、テキストボックス、コンボボックス、テキストボックスをこの順序で持っています:

this.columnLocalName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.columnLocalAddress = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.columnLocalPort = new System.Windows.Forms.DataGridViewTextBoxColumn();   

そして、それらは次のように datagridview に表示されます。

this.dataGridViewLocalProfile.Columns.AddRange(
new System.Windows.Forms.DataGridViewColumn[] {
                    this.columnLocalName,
                    this.columnLocalAddress,
                    this.columnLocalPort});

後で、次のように各コンボボックス セルに異なる値を追加しようとします。

foreach (profile in localProfile.List)
{
DataGridViewComboBoxCell cell =(DataGridViewComboBoxCell)
(dataGridViewLocalProfile.Rows[dataGridViewLocalProfile.Rows.Count - 1].
Cells["columnLocalAddress"]);

cell.Items.Clear();
cell.Items.Add(profile.Address.ToString());

dataGridViewLocalProfile.Rows.Add(
new string[] { profile.Name, profile.Address, profile.Port });
}

これにより、最初の列と最後の列が入力され、コンボボックス列が空のデータグリッドが生成されます。私が扱うデータエラーで。メッセージは次のとおりです。

DataGridViewComboBoxCell value is not valid.

ほとんどの投稿を読みましたが、これに対する解決策が見つかりません。

次のようにデータソースを設定してみました:

cell.DataSource = new string[] { profile.Address };

dataerror と言ってまだ空のコンボボックス列を取得しています

DataGridViewComboBoxCell value is not valid.

各コンボボックスセルに異なる値を追加するので、これは非常に難しいと思います。

誰でも、どうすればこれを機能させることができるか教えてください。

/一番

4

1 に答える 1

0

ゲームに遅れましたが、とにかくここに解決策があります.

問題はforeachループにあります。ComboBox最後の既存の行のセルにアイテムが入力されます。しかし、その後、現在のprofileオブジェクトを使用してまったく新しい行が追加されます。

dataGridViewLocalProfile.Rows.Add( new string[] { profile.Name, profile.Address, profile.Port });

この新しい行のセルの項目ComboBoxは空であるため、profile.Address 有効ではありません。ループを次のように変更するforeachと、ゴールドになります。

foreach (Profile p in this.localProfile)
{
  DataGridViewRow row = new DataGridViewRow();
  row.CreateCells(this.dataGridView1);

  DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)row.Cells[1];
  cell.Items.Clear();
  cell.Items.Add(p.Address);

  row.Cells[0].Value = p.Name;
  row.Cells[1].Value = p.Address;
  row.Cells[2].Value = p.Port;
  this.dataGridView1.Rows.Add(row);
}
于 2015-04-07T23:07:46.563 に答える