DataGridViewComboBoxColumn 绑定对象集合时存在BUG?

     DataGridview 绑定的是对象集合,里面有一列是下拉列表,也用集合绑定,一切看上去很美好,可是当我改变选择下拉列表时,却发生BUG。字符串不能转成对象!不知是DataGridview里的对应的下拉列表列保存的是字符串,还是下拉列表保存的是字符串。

     DataGridViewComboBoxColumnError1.jpg

   查google 得知,下拉列表是以模板的方式进行编辑,这有点以前web      下的datagrid

(既然编辑的时候用另一种控件,为何显视时不直接使用label算了,就象Infragistics UltraGrid)。于是创建一下模板列继承DataGridViewComboBoxCell。果然有ParseFormattedValue这个方法。跟踪,原来formattedValue传进来时是一个字符串类型。看来问题很容易解决吗,于是转成对象:

None.gif    public   override   object  ParseFormattedValue( object  formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif              ComboBox comboBox 
= base.DataGridView.EditingControl as ComboBox;
InBlock.gif              formattedValue 
= (BizObject)comboBox.SelectedItem;
InBlock.gif            
return base.ParseFormattedValue(formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
ExpandedBlockEnd.gif        }

None.gif


这下好了,出现了新的问题
DataGridViewComboBoxColumnError2.jpg

类型转化错误!由于调用了内部类,这下子跟踪不了了,反编译System.Windows.Forms,得到下面方法:

None.gif public   override   object  ParseFormattedValue( object  formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif      
if (valueTypeConverter == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
if (this.ValueMemberProperty != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  valueTypeConverter 
= this.ValueMemberProperty.Converter;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else if (this.DisplayMemberProperty != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  valueTypeConverter 
= this.DisplayMemberProperty.Converter;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif      }

InBlock.gif      
if (((this.DataManager != null&& ((this.DisplayMemberProperty != null|| (this.ValueMemberProperty != null))) || (!string.IsNullOrEmpty(this.DisplayMember) || !string.IsNullOrEmpty(this.ValueMember)))
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
object obj1 = base.ParseFormattedValueInternal(this.DisplayType, formattedValue, cellStyle, formattedValueTypeConverter, this.DisplayTypeConverter);
InBlock.gif            
object obj2 = obj1;
InBlock.gif            
if (!this.LookupValue(obj2, out obj1))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                  
if (obj2 != DBNull.Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
throw new FormatException(string.Format(CultureInfo.CurrentCulture, SR.GetString("Formatter_CantConvert"), new object[] dot.gif{ obj1, this.DisplayType }));
ExpandedSubBlockEnd.gif                  }

InBlock.gif                  
return DBNull.Value;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return obj1;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
return base.ParseFormattedValueInternal(this.ValueType, formattedValue, cellStyle, formattedValueTypeConverter, valueTypeConverter);
ExpandedBlockEnd.gif}

None.gif
None.gif
internal   object  ParseFormattedValueInternal(Type valueType,  object  formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif      
if (cellStyle == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
throw new ArgumentNullException("cellStyle");
ExpandedSubBlockEnd.gif      }

InBlock.gif      
if (this.FormattedValueType == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
throw new FormatException(SR.GetString("DataGridViewCell_FormattedValueTypeNull"));
ExpandedSubBlockEnd.gif      }

InBlock.gif      
if (valueType == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
throw new FormatException(SR.GetString("DataGridViewCell_ValueTypeNull"));
ExpandedSubBlockEnd.gif      }

InBlock.gif      
if ((formattedValue == null|| !this.FormattedValueType.IsAssignableFrom(formattedValue.GetType()))
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif            
throw new ArgumentException(SR.GetString("DataGridViewCell_FormattedValueHasWrongType"), "formattedValue");
ExpandedSubBlockEnd.gif      }

InBlock.gif      
return Formatter.ParseObject(formattedValue, valueType, this.FormattedValueType, (valueTypeConverter == null? this.ValueTypeConverter : valueTypeConverter, (formattedValueTypeConverter == null? this.FormattedValueTypeConverter : formattedValueTypeConverter, cellStyle.FormatProvider, cellStyle.NullValue, cellStyle.IsDataSourceNullValueDefault ? Formatter.GetDefaultDataSourceNullValue(valueType) : cellStyle.DataSourceNullValue);
ExpandedBlockEnd.gif}

None.gif
 

在方法ParseFormattedValueInternal 里有一个属性:  this.FormattedValueType 是字符串型,而我传进去的已经变成了对象型了,所以在这里报错了。

 

属性 FormattedValueType覆盖了基类, 这里已经变成了字符串类型

public   override  Type FormattedValueType
{
      
get
      {
            
return  DataGridViewComboBoxCell.defaultFormattedValueType;
      }
}
 
 
[Browsable( false )]
public   virtual  Type FormattedValueType
{
      
get
      {
            
return   this .ValueType;
      }
}

于是用这个基类 FormattedValueType重新覆盖。结果错误是没有了,可下拉列表列全是空白。不过我点击下拉列表,却是程序需要的选择。看来显示又出现了问题.

 

最后的解决方法是:BizObject 是我对象的基类

   public   override   object  ParseFormattedValue( object  formattedValue, DataGridViewCellStyle cellStyle, TypeConverter formattedValueTypeConverter, TypeConverter valueTypeConverter)
        {
            ComboBox comboBox 
=   base .DataGridView.EditingControl  as  ComboBox;
            formattedValue 
=  (BizObject)comboBox.SelectedItem;
            
return  formattedValue;

        }

虽然没有问题了,但是直接反回而忽略其它几个参数,感觉不是很自然。不知大家有没有更好的方法。

转载于:https://www.cnblogs.com/mediar/archive/2006/08/28/488117.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值