Windows Forms DataBinding displays ‘System.Data.DataRowView’ in text boxes and other controls

ERROR

Windows Forms DataBinding displays ‘System.Data.DataRowView’ in text boxes and other controls

SOLVED

Was a ComboBox databinding pointing at a field no longer in the dataset.

Entity Framework possible feedback to Microsoft Connect – IBindingListView

 

https://connect.microsoft.com/data/feedback/details/704105/entity-framework-winforms-datagridview-support-for-bindingsource-filter-sort-and-column-heading-sort

 

Entity Framework WinForms DataGridView support for BindingSource.Filter .Sort and column heading Sort

Entity Framework looks useful and I have limited use of it in my code. Also several developers have expressed to me how they have found it useful.

I currently use a lot of WinForms databinding using DataSets and BindingSource. Out of the box I get column header sorting. Also it is a simple matter to set BindingSource.Filter and BindingSource.Sort as strings. I have used this feature to provide a lot of data-driven filter and sort strings for UI quick filter and sort which works very well.

However when I use Entity Framework the column header sort and BiningSource.Filter and .Sort properties do not work. My understanding is that this is because EF does not support IBindingListView. Although it might but not out of the box. Please advise or consider implementing this in future editions of Entity Framework.

Thank you

Stephen, UK

Entity Framework: WinForms databinding in EF4.1 – DBSet

Solved

Dim context As New SOPEntities
System.Data.Entity.DbExtensions.Load(context.Employees.Where(Function(f) f.Current = True).OrderBy(Function(g) g.FullName))
EmployeeBindingSource.DataSource = context.Employees.Local
ListBox1.DataSource = System.Data.Entity.DbExtensions.Tobindinglist(context.Employees.Local)
ListBox1.DisplayMember = “FullName”

Problem

‘local’ is not a member of ‘System.Data.Objects.ObjectSet(Of <entityname>)’

See http://wp.me/p17IS4-ih

 

Solved

Open the <Name>Model.edmx > Right click > Add code generation item…

Choose ADO.Net DbContext Generator. This has affect of changing the .edmx file Code Generation Strategy to “None” and creates a text template model and .Local now works.

Discussion

Uses EF4.1 EntityFramework.dll
Database first, then add code generation item to get T4 simpler classes and DBSet inherits DBContext
Initially could not work out how to get the Load method. See above for solution.
There is no MSDN VB.Net sample yet. The C# is more like context.Employees.Local.ToBindingList. So it took me a while to work out the syntax above.

Unsolved:
Also the FullName is now a computed column in the database. Was unable to work out how to do this in EF. It may come in the future as a “Model Defined Function”. Do not think this is a Scalar or Complex Type which is something else.
Unsolved:
For BindingSource to support complex filter it needs to support IBindingListView. Typically EF does not.

Links

Binding Objects to Controls
http://msdn.microsoft.com/en-us/library/gg197521(v=VS.103).aspx

Following is in C#
http://msdn.microsoft.com/en-us/library/gg197523(v=VS.103).aspx

Set ComboBox to an item when bound to objects

Use Object.Equals.
Then set the combobox selecteditem to a new instance of the object with the required ID or other.
Sample Equals

Public Overrides Function Equals(obj As Object) As Boolean
    If obj Is Nothing Then
        Return False
    End If
    Return CType(obj, Customer).ID = Me.ID
End Function

Binding a setting to a Property at Design Time. Windows Forms

 

Window form and control properties may be bound to Application Settings, by expanding the Application Settings at the top of the control properties.
This works fine, for say BackColor. However be warned, there is then a marker by the related bound property. If you then set this manually, this is actually bi-directional and you will change it in the Settings file at the same time, and hence all accross your application, which is probably not your intended action.

See Microsoft Book 70-505 Windows Forms p 587

DateTimePicker CheckBox for setting Null

Position is important. Needs to be before EndEdit

Me.TimeSheetBindingSource.EndEdit
If Me.TimeEndDateTimePicker.Checked = False Then
  rowTS = CType(CType(Me.TimeSheetBindingSource.Current, DataRowView).Row, dsMyCo.TimeSheetRow)
  rowTS.SetTimeEndNull()
End

Set ToolStripTextBox Focus when Opening the form

me.ActiveControl=me.ToolStripTextBox.Control
You also need to set the ToolStrip’s (not the ToolStripTextBox’s – it doesn’t have the property) TabStop property to True.
Also if you set this active control on form load, then if you hide the form you may change the focus when it becomes visible again.
Could use following

PrivateSub frm_VisibleChanged(sender AsObject, e As System.EventArgs) HandlesMe.VisibleChanged

If Me.Visible Then
           ‘ToolStrip1.TabStop = True
            ActiveControl = InputProductToolStripTextBox.Control
   End If

EndSub