2013-10-21 101 views
6

我已经看到了很多涉及数据绑定的解决方案,但我没有数据源。在这种情况下,组合单元格仅适用于1行(其他行不具有DataGridViewComboBoxCell)。如何捕获DataGridView验证在编辑单元格中写入值时出错?

我设置DataGridViewComboCell像这样:

DataGridViewComboBoxCell cell = new DataGridViewComboBoxCell(); 
cell.DisplayStyle = DataGridViewComboBoxDisplayStyle.ComboBox; 
cell.Items.AddRange(items.ToArray()); // items is List<string> 

我动态后重新填充它是这样的:

_cell.Items.Clear(); 
_cell.Items.AddRange(this.Data.ResponseOptions.Select(d => d.Description).ToArray()); 
//d.Description is of type string 

但后来我得到这个讨厌的对话框,上面写着:

DataGridView中发生以下异常: System.ArgumentException:DataGridViewComboB oxCell值无效。 要替换此默认对话框,请处理DataError事件。

顺便说一句,它并没有多大帮助,说它不是“有效的”。向MS发送电子邮件称Windows Forms无效是否公平?

我试过抓住单元格的项目属性,并添加使用foreach()与Add()调用的字符串。我仍然得到对话。

我也试过每次我想更新它并重新创建一个新的DataGridViewComboCell时吹走整个单元格。我仍然得到对话。

我也尝试手动覆盖列值(成功时,我没有这个问题)。虽然没有解决它。

当我尝试重新填充组合单元中的项目时,我似乎只能看到此对话框。

现在我只是抹去了DataError方法。

有什么建议吗?

+1

我以前见过这个错误。它发生在为单元格设置了* ValueType *并且您提供的数据类型不同的情况下。你检查过了吗? – ThunderGr

+0

@ThunderGr:这也是我的想法,但正如你从上面看到的,我尝试在初始和更新中(以几种方式)将项目添加为完全相同的类型。 – micahhoover

+1

如果我是你,我会让DataError打印一份关于添加的项目类型和值的消息框的报告。很多时候你认为你做的事与你实际做的不一样。特别是在C#中。我怀疑是空洞,说实话。 – ThunderGr

回答

1
  1. 使用WPF。

  2. 将DataGrid中的组合框列绑定到列表。

  3. 唱一首歌 - 你会到之后你才意识到在WPF中这是多么容易实现!

+0

啊,根本原因分析:)我有权将它放在建议框中,就是这样。 – micahhoover

+0

我想知道是否可能通过清空组合框,因为列的值不再在组合框中找到,它可能会导致错误/异常? –

+0

我应该提到(1)我也手动覆盖了这个值,(2)这只是DGV中的一行。其他行没有组合单元。 – micahhoover

3

这个问题有点老了,但我遇到了这个,事实证明,雷霆GR绝对正确。当我有DataError打印报告时,我开始查看数据库中的值,并注意到它们实际上都是CAPS。只需将我的字符串值更改为全部大写,不再收到错误。

这是一种可能处理DataError的方法,尽管我宁愿不把它放在第一位。

private void dataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e) 
    { 
     try 
     { 
      if (e.Exception.Message == "DataGridViewComboBoxCell value is not valid.") 
      { 
       object value = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value; 
       if (!((DataGridViewComboBoxColumn)dataGridView.Columns[e.ColumnIndex]).Items.Contains(value)) 
       { 
        ((DataGridViewComboBoxColumn)dataGridView.Columns[e.ColumnIndex]).Items.Add(value); 
       } 
      } 

      throw e.Exception; 
     } 
     catch (Exception ex) 
     { 
      bool rethrow = ExceptionPolicy.HandleException(ex, "UI Policy"); 
      if (rethrow) 
      { 
       MessageBox.Show(string.Format(@"Failed to bind ComboBox. " 
       + "Please contact support with this message:" 
       + "\n\n" + ex.Message)); 
      } 
     } 
    } 

这种方式值会被添加到items集合中,但仍然会抛出可以记录的异常。我使用了一个消息框,因为我仍然认为用户体验错误消息并联系支持部门修复它是很重要的。希望这可以帮助别人!

1

我今天面临同样的例外,花了很多时间来搞清楚这个问题。事实证明,当为datagridview创建一个新行时,我调用了一个事件处理程序。在该事件处理程序方法中,我错误地将组合框单元值设置为comboxox项目集合中不存在的值。

因此,总之,抛出的异常显示正确的信息。我只是要找到确切的位置,错误的值被设置在。

+1

太好了。即使我正面临着这个错误。 – RBT

0

我有同样的问题,并解决它添加到列表中,我分配给数据源一个项目对应的组合框中的空项目。

例如,

var list = controller.GetAllMovementKind(); 
list.Add(new Model.MovementKind { Code = 0, Name = String.Empty }); 
movementKindBindingSource.DataSource = list; 

GetAllMovementKind方法返回一个List<Model.MovementKind>

我希望它帮你。

1

与其添加到DataGridViewComboBoxColumn.Items,您需要将DataGridViewComboBoxColumn.DataSource设置为ResponseOptions的List,并设置DataGridViewComboBoxColumn.ValueType = typeof(ResponseOption)。

0

要管理和验证在DataGridView中编辑数据手动添加后续事件:

myDataGridView.DataError += myDataGridView_DataError; 

和实施这样的事件:在这个例子中

private void dataGridApprovazioni_DataError(object sender, DataGridViewDataErrorEventArgs e) 
{ 
    DataGridView dataGridView = (DataGridView)sender; 
    dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = DateTime.MinValue; 
    MessageBox.Show("Invalid Date Format", "System Info", MessageBoxButtons.OK, MessageBoxIcon.Error); 

} 

的管理控制OD日期时间场在e特定列表索引中。

相关问题