DisplayName DataAnnotations not working in WinForms 3.5 DataGridView
Ok, I'm completely at a loss here. I've used DataAnnotations attribute DisplayName successfully using MVC model binding, and even with WPF/Silverlight model binding and of course it works just fine, but now I'm on a project that I'm forced to use VB.NET 3.5 WinForms.
I have a Linq2Sql model and I created a partial class for one of my classes and included a MetadataType attribute to point to a metadata class. I added a DisplayName attribute to a property in the metadata class. I then bind my datagridview with an IQueryable(Of mydatatype), but the column name in the grid is the Property's name and not the DisplayName.
Am I missing something? Is there something else I need to do to get the datagridview to use the DisplayName?
In my Model class:
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
<MetadataType(GetType(vwREVIEW_ECRMetadata))> _
Partial Class vwREVIEW_ECR
Public Sub TestMethod()
End Sub
End Class
Public Class vwREVIEW_ECRMetadata
Private _ECRNumber As String
<DisplayName("ECR #")> _
Public Property ECRNumber() As String
Get
Return _ECRNumber
End Get
Set(ByVal value As String)
_ECRNumber = value
End Set
End Property
End Class
In my Repository class:
Public Function GetAllECR开发者_JAVA技巧sForLookup() As IQueryable(Of vwREVIEW_ECR)
Return db.vwREVIEW_ECRs
End Function
In my Presenter class:
Public Sub GetData()
view.FillData(model.GetFilteredECRsForLookup())
End Sub
In my View:
Public Sub FillData(ByVal data As System.Linq.IQueryable(Of vwREVIEW_ECR)) Implements ILookupECRView.FillData
Me.uxECRData.DataSource = data
End Sub
Any help would be greatly appreciated! Thanks
Ok, so I found a solution to my problem. Didn't even think about it this way, but in ASP.NET & WPF, you get this behavior becuase of model binding behavior built in. WinForms has databinding as well, but isn't just there for your. Though I could've "bound" my datagridview to my linq2sql generated object in the runtime, which would've accomplished what I needed, I needed to do this at design time, so instead, I modified my MVP to use ViewModels where needed, and bind the datagrid to that object at runtime to get my column names to look the way I want. The ViewModel is hooked up to the model and can pass the real values to it.
I based this approach on this blog, though I didn't fully implement what he did: http://aviadezra.blogspot.com/2009/08/mvp-mvvm-winforms-data-binding.html
精彩评论