DataGridViewComboBoxCell值无效 [英] DataGridViewComboBoxCell value is not valid

查看:149
本文介绍了DataGridViewComboBoxCell值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Howdy



我有一个DataGridView,其中包含2个带有父子关系的ComboBox列。一个列出客户端,其他客户端项目。



每个ComboBox都绑定到自己的绑定源, bsClients bsClientProjects bsClientProjects bsClients_CurrentChanged 事件上刷新。



这在收集数据时工作正常第一次,但在将数据加载回DataGridView时, DataGridViewComboBoxCell值无效错误。



我怎么能拥有客户端项目ComboBox在DataGridView绑定过程中刷新其列表?



干杯

Craig



PS使用针对类型化数据集执行的linq填充绑定源。

Howdy

I have a DataGridView containing 2 ComboBox columns with a parent child relationship. One lists Clients, the other Client Projects.

Each ComboBox is bound to its own binding source, bsClients and bsClientProjects, with bsClientProjects being refreshed upon the bsClients_CurrentChanged event.

This works fine when collecting data for the first time, but breaks with a DataGridViewComboBoxCell value is not valid error when loading the data back into the DataGridView.

How can I have the the Client Project ComboBox refresh its list during the DataGridView binding process?

Cheers
Craig

P.S. The binding sources are populated using linq executed against typed datasets.

推荐答案

很好的答案

这帮我很多

Nice answer
This helped me alot
((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Add(value);





谢谢

Khalid

为单元格设置一个空值:



var combobox =(DataGridViewComboBoxCell)dataGridView.CurrentRow.Cells [NAME];

combobox.DataSource = null;

combobox.Items.Clear();

combobox.ValueType = typeof(string)



dataGridView.CurrentRow.Cells [NAME] .Value = null;
Set a null value to the cell:

var combobox = (DataGridViewComboBoxCell)dataGridView.CurrentRow.Cells[NAME];
combobox.DataSource = null;
combobox.Items.Clear();
combobox.ValueType = typeof(string)

dataGridView.CurrentRow.Cells[NAME].Value = null;


只是转到ur datagridview控件的DataError事件

并写下以下代码



just go to the DataError event of ur datagridview control
and write the following code in it

if (e.Exception.Message == "DataGridViewComboBoxCell value is not valid.")
           {
               object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
               if (!((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Contains(value))
               {
                   ((DataGridViewComboBoxColumn)dataGridView1.Columns[e.ColumnIndex]).Items.Add(value);
                   e.ThrowException = false;
               }
           }


这篇关于DataGridViewComboBoxCell值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆