2

同様の問題を抱えているトピックをいくつか見つけましたが、内容が少し異なっているようで、結果として問題を解決できませんでした。

私のアプリケーションには、4つの列を含むDataTableがあります。

  • UInt16
  • UInt64
  • UInt64
  • 2つの値で構成される自己定義の列挙型。

このDataTableの値を表示するために、DataGridViewを作成し、このテーブルをデータソースとして設定しました。ここまでは順調ですね。代わりに、列挙フィールド列にコンボボックスを含めて、DataGridViewComboBoxColumnタイプに遭遇したかったのです。私はそれを機能させるのにいくつかの問題がありましたが、最終的には次のアプローチを使用することになりました:

// Create the 4 datarows

// Add the datarows to the data table

// Set the data table as the data source for the data grid view

// Remove the column that represents the enumeration

// Add a DataGridViewComboBoxColumn to the DataGridView as replacement

次のようにDataGridViewComboBoxColumnを作成しました。

DataGridViewComboBoxColumn cb = new DataGridViewComboBoxColumn();
cb.HeaderText   = "My header text";
cb.ValueType    = typeof(MyEnumType);
cb.DataSource   = Enum.GetValues(typeof(MyEnumType));
cb.FlatStyle    = FlatStyle.System;
cb.Name         = "Name"; //Same name as the DataColumn and the now deleted DataGridViewColumn
cb.DataPropertyName = "Name"; //Same name as the DataColumn and the now deleted DataGridViewColumn
cb.DisplayStyle     = DataGridViewComboBoxDisplayStyle.Nothing;

dataGridView.Columns.Add(cb);

アプリケーションが起動したら、上記の4つのデータ型のフィールドを持つ構造に配置されるテキストファイルからデータを読み込みます。次に、これらのフィールドを次のようにDataTableに追加します。

DataRow row = dataTable.NewRow();
row["Name of the UInt16 column"]    = mystruct.theUInt16;
row["Name of the UInt64 column"]    = mystruct.theUInt64;
row["Name of the UInt64 column"]    = mystruct.theUInt64_2;
row["Name of the enum column"]      = mystruct.theEnumValue;               
dataTable.Rows.Add(row);

起動時に、DataErrorイベントが繰り返し呼び出されます。ただし、セルの内容は適切に入力されます。(エラーを数回クリックすると、これが表示されます)DataErrorイベントを無効にする(たとえば、空のハンドラーを割り当てる)ことは、私がしたくないことです。

どういうわけか、ある種の型の不一致があると思います。(おそらく列挙型と表示用の文字列ですか?)ただし、これは単なる推測です。dataTable列とdatagridview列はどちらも、タイプが列挙型に設定されています。

誰かが私を正しい方向に向けてくれることを願っています。

前もって感謝します!

4

3 に答える 3

1

最後の列を追加および削除する代わりに、ColumnAddedイベントをリッスンしてDataGridViewからタイプを変更する方法は次のとおりです。

dataGridView.ColumnAdded += DataGridViewColumnAdded;
dataGridView.DataSource = dataTable;

private void DataGridViewColumnAdded(Object sender, DataGridViewColumnEventArgs e) 
{
   if(e.Column.ValueType == typeof(MyEnumType)) 
   {
      DataGridViewComboBoxCell cb = new DataGridViewComboBoxCell();
      cb.ValueType        = typeof(MyEnumType);
      cb.DataSource       = Enum.GetValues(typeof(MyEnumType));
      cb.FlatStyle        = FlatStyle.System;
      cb.DisplayStyle     = DataGridViewComboBoxDisplayStyle.Nothing;
      e.Column.CellTemplate = cb;
   } 
}
于 2012-05-03T13:07:50.623 に答える
0

私は自分のニーズに合った回避策を見つけることができました。アプローチは、私の元の投稿と同じです。DataTableの列挙型に「colMyEnumTypeTable」という名前を付け、データ型はMyEnumTypeではなく文字列にしました。次に、この列を削除し、「colMyEnumType」という名前の DataGridViewComboBoxColumn を DataPropertyName 「colMyEnumTypeTable」で追加し、文字列型も追加しました。次に、データ テーブルに追加するときに列挙値を文字列に変換し、抽出するときに Enum.Parse を使用して列挙値を再度取得します。もう DataError 呼び出しは行われません。

-編集- DataTable と DataGridViewComboBoxColumn に異なる名前を付けて、問題が発生していないことを確認しました。私は今それらを元に戻したので、それらは同じ名前になり、まだ機能します。

-edit2- そして、comboboxcolumn の DataSource プロパティを設定する代わりに、列挙値を Items プロパティに追加して追加しました。(文字列に変換後)

于 2012-05-04T07:46:17.783 に答える