从控件的DataBindings获取EditValue

我有TextEdit Control ,它绑定到Datasource的类字段,所以它看起来像EditValue - bindingSource1.MyClassField 。 我可以通过代码以某种方式获得EditValue的类型吗?

我发现textEdit1.DataBindings[0].BindableComponent具有我需要的EditValue,但它似乎是私有的,而textEdit1.DataBindings[0].BindingMemberInfo只包含字符串值。

在此处输入图像描述

你必须将bindingsource current转换为DataRowView。

 object Result = null; DataRowView drv = bindingSource1.Current as DataRowView; if (drv != null) { // get the value of the field Result = drv.Row["columnName"]; // show the type if this value MessageBox.Show(Result.GetType().ToString()); } 

由于这从绑定源获取值,因此绑定到哪个控件并不重要。 它适用于TextEdits以及DataGridView。

编辑:
另一种获取类型的方法是:(这只有在EditValue不为null时才有效)

 object test; test = textEdit1.EditValue; MessageBox.Show(test.GetType().ToString()); 

编辑:
我找到了一种从数据绑定中查找信息的方法,因此当textEdit1的EditValue仍为null时它也应该有效。

 PropertyDescriptorCollection info = textEdit1.DataBindings[0].BindingManagerBase.GetItemProperties(); string fieldname = textEdit1.DataBindings[0].BindingMemberInfo.BindingField; MessageBox.Show(info[fieldname].Name + " : " + info[fieldname].PropertyType.ToString()); 

我在这个页面上找到了这个:
https://msdn.microsoft.com/en-us/library/kyaxdd3x(v=vs.110).aspx#Examples