Jon
Jon

Reputation: 40032

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 to 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

Upvotes: 1

Views: 1191

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062865

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;}
}

Upvotes: 1

Related Questions