开发者

Winforms, BindingList, BindingSource - Cannot bind to property/column Id

I have the following code on a form:

Public Sub Init(ByVal SelectedItems As List(Of Cheque))
        Items = New BindingList(Of Cheque)(SelectedItems )
        BindingSource1.DataSource = Items 
        txtNo.DataBindings.Add("Text", Items, "Number")
        txtChequeAmount.DataBindings.Add("Text", Items, "Amount")
        lbId.DataBindings.Add("Text", Items, "Id")
    End Sub

This code gets called like so:

...
fmEdit.Init(myList)
fmEdit.Show()

All variables are populated etc, it seems to go through the DataBindings.Add ok but when the form appears I get the error about unable to bind to a property or column called Id. I tried replacing the DataBindings.Add code t开发者_Go百科o use the BindingSource1 instead of Items but I get a similar error.

The names of the properties in the class match that of the name in the Databindings.Add code.

Any idea?

Thanks

UPDATE: Here is my class:

Public Class Cheque
    Public Id As String
    Public Status As Byte
    Public Amount As String
    Public Number As String
End Class


In your example code, you show public fields, which are generally a very bad idea. Unless you try very hard (it can be done), data binding applies only to properties. So change those fields to properties and it will work.

IIRC in vb.net this is:

Public Class Cheque
    Public Property Id As String
    Public Property Status As Byte
    Public Property Amount As String
    Public Property Number As String
End Class

(but my vb is rusty)

or in C#:

public class Cheque {
    public string Id {get;set;}
    public byte Status {get;set;}
    public string Amount {get;set;}
    public string Number {get;set;}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜