同様の問題を抱えているトピックをいくつか見つけましたが、内容が少し異なっているようで、結果として問題を解決できませんでした。
私のアプリケーションには、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列はどちらも、タイプが列挙型に設定されています。
誰かが私を正しい方向に向けてくれることを願っています。
前もって感謝します!