我有一個綁定到一個枚舉組合框列一個DataGridView如下:檢查DataGridViewComboBox是空
var D = (DataGridViewComboBoxColumn)dgvInputs.Columns[2];
D.ValueType = typeof(MyType);
D.ValueMember = "Value";
D.DisplayMember = "Display";
D.DataSource = new MyType[] {
MyType.Rev,
MyType.Model,
MyType.User,
MyType.Status
}.Select(x => new { Display = x.ToString(), Value = (int)x }).ToList();
在DataGridView然後綁定到數據表命名ParameterTable:
BindingSource ParamSource = new BindingSource();
ParamSource.DataSource = DataEntry.ParameterTable;
dgvInputs.AutoGenerateColumns = false;
dgvInputs.DataSource = ParamSource;
dgvInputs.Columns[0].DataPropertyName = "Name";
dgvInputs.Columns[1].DataPropertyName = "Prompt";
dgvInputs.Columns[2].DataPropertyName = "Type";
dgvInputs.Columns[3].DataPropertyName = "Width";
dgvInputs.Columns[4].DataPropertyName = "Default Value";
當用戶完成編輯表格時,我需要驗證它。特別是,我需要測試在每行中定義了類型,並且默認值與類型兼容。
問題是,我發現用於檢查類型是否已經設置的每個測試都失敗了。當我稍後嘗試將值作爲MyType轉換爲測試默認值的一部分時,出現錯誤。當我在調試器的空白類型單元格中檢查.Value屬性時,它顯示值「{}」。
目前,我有用於測試的代碼,在DataGridView自身的Validating事件中。我曾嘗試過各種其他版本,他們也都失敗了:
foreach (DataGridViewRow Row in dgvInputs.Rows) {
if (!Row.IsNewRow) {
// test other columns ...
DataGridViewComboBoxCell Cell = (DataGridViewComboBoxCell)(Row.Cells[2]);
if (Cell == null || Cell.Value as string == string.Empty) {
// Error ...
}
MyType PType = (MyType)(Cell.Value);
如何測試是否DataGridViewComboBox細胞尚未確定,而這是什麼價值「{}」?
僅供參考 - 我使用VS 2008和.Net 3.5 SP1。不是我的選擇。正是我所能得到的。
什麼是'DataEntry.ParameterTable'的類型? –
對不起。這是一個數據表 –
if(Cell.Value == null)應該告訴什麼時候沒有設置單元 –