问题
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?
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.
回答1:
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:
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());
EDIT:
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
来源:https://stackoverflow.com/questions/44839836/get-editvalue-from-controls-databindings