当将DataGridView组合框更改为默认值时,它会抛出异常 [英] when changing DataGridView combobox to Default Value, it throws exception
问题描述
我有一个winform应用程序有一个datagridview与组合框。 Combobox是显示模型类型的数据绑定。如果我加载以前保存的数据在gridview,并尝试选择combobox第一个成员是NullValue(--Select--)它抛出异常无法设置列'ModelTypeID'为null。请改用DBNull。。完整异常消息如下所示。
在System.Data.DataRow.set_Item(DataColumn列,对象值)
在System.Data.DataRowView.SetColumnValue(DataColumn列,对象值)
在System.Data.DataColumnPropertyDescriptor.SetValue(对象组件,
对象值)
在System.Windows.Forms.DataGridView .DataGridViewDataConnection.PushValue
(Int32 boundColumnIndex,Int32 columnIndex,Int32 rowIndex,Object value)
您可以查看此解决方法提及此处
它涉及订阅DataTable绑定的列更改事件
void dt_ColumnChanging(object sender,DataColumnChangeEventArgs e)
{
if(e.Column == dt.Columns [myColumn])
{
if(e.ProposedValue == null)
{
e.ProposedValue = DBNull.Value;
}
}
}
I have a winform app which has one datagridview with combobox. Combobox is databound which displays model types. if i load previously saved data on gridview and try to select combobox first member which is NullValue (--Select--) it throws exception Cannot set Column 'ModelTypeID' to be null. Please use DBNull instead.. Full exception message is given below.
at System.Data.DataRow.set_Item(DataColumn column, Object value)
at System.Data.DataRowView.SetColumnValue(DataColumn column, Object value)
at System.Data.DataColumnPropertyDescriptor.SetValue(Object component,
Object value)
at System.Windows.Forms.DataGridView.DataGridViewDataConnection.PushValue
(Int32 boundColumnIndex, Int32 columnIndex, Int32 rowIndex, Object value)
can anyone suggest me any workaround for this?
thanks
You can have a look at this workaround mentioned here
It involves subscribing to the column changing event for the DataTable bound
void dt_ColumnChanging(object sender, DataColumnChangeEventArgs e)
{
if (e.Column == dt.Columns["myColumn"])
{
if (e.ProposedValue == null)
{
e.ProposedValue = DBNull.Value;
}
}
}
这篇关于当将DataGridView组合框更改为默认值时,它会抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!