本文介绍了从控件的DataBindings获取EditValue的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 TextEdit控件,它绑定到数据源中的类字段,因此它看起来像 EditValue-bindingSource1.MyClassField 。我可以通过代码以某种方式获得EditValue的类型吗?

I have TextEdit Control and it is bound to a class field from Datasource, so it is looks like EditValue - bindingSource1.MyClassField. Can I somehow get the type of EditValue via code?

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

I've found that textEdit1.DataBindings[0].BindableComponent has EditValue that I need, but it seems to be private, and textEdit1.DataBindings[0].BindingMemberInfo contains only string values.

推荐答案

您必须强制转换绑定源当前为DataRowView。

you have to cast the bindingsource current to a 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());
}

因为这是从绑定源获取值,所以绑定什么控件都没有关系对此。

Since this gets the value from the bindingsource it does not matters what controls are binded to it. It will work for TextEdits just as well as for a DataGridView.

EDIT:

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


Another way to get the type is this :(this will only work if EditValue is not null)

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

编辑:

我找到了一种方法从数据绑定中查找信息,因此当textEdit1的EditValue仍然为null时,它也应该起作用。


I found a way to find the information from the databinding, so it should also work when the EditValue of textEdit1 is still null.

PropertyDescriptorCollection info = textEdit1.DataBindings[0].BindingManagerBase.GetItemProperties();
string fieldname = textEdit1.DataBindings[0].BindingMemberInfo.BindingField;

MessageBox.Show(info[fieldname].Name + " : " + info[fieldname].PropertyType.ToString());

我在此页面上找到此内容:

I found this on this page:
https://msdn.microsoft.com/en-us/library/kyaxdd3x(v=vs.110).aspx#Examples

这篇关于从控件的DataBindings获取EditValue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 07:16