Search code examples
c#datagridviewbindinglist

some of my class<T>'s members don't turn up on datagridview bound to BindingList<T>


public class ANote{
    public string NoteId = "";
    public string Note = "";
    public string NoteCollector = "";
    public DateTime NoteCollectionDate = DateTime.MinValue;
    public string NoteCollectionDay {
    get { return NoteCollectionDate.toString("MM/dd/yyyy") ; }
    }
    public string NoteCollectionTime {
    get { return return NoteCollectionDate.toString("hh/mm tt"); }
    }
    public DateTime ADate = DateTime.Now;
    public double AAmount = 0.0D;
    public string AName = "";
    }

And a BindingList aList;

Also have a grid with a bunch of DataGridTExtBoxColumns that I try to bind to the above (already populated) List like :

colDate.DataPropertyName ="NoteCollectionDay";
colTime.DataPropertyName = "NoteCollectionTime";
colName.DataPropertyName = "NoteCollector";
colADate.DataPropertyName = "ADate";
colAAmount.DataPropertyName = "AAmount";
colAName.DataPropertyName = "AName";
colNotes.DataPropertyName = "Note";

grdNotes.AutoGenerateColumns = false;
grdNotes.DataSource = aList;

But at runtime, only my colDate and colTime columns populate properly. All others are blank.. When I look specifically at the Grid.Rows[idx].Cells[idx].Value for the other colmns it's all null.

Also if I set AutoGenerateColumns to true, I see an additional column NoteID and that is also filled properly, but the ANote, Amount, ADate, AName and Note fields are still blank !

There is nothing wrong with the data in the list .. all the class members have valid values.

Unless I'm missing something It seems to be an issue with BindingList or DataGridView .. If not, any ideas on how to debug this..it's a pretty simple testcase !


Solution

  • You are referring to a DataPropertyName, hence only properties will work?

    The rest are fields. Try converting them to auto properties:

    public string Note { get; set; }
    

    Also please note that a BindingList will only notify subscribers that the content of the list itself has changed, not the Property of an object contained in the list.

    If you want to achieve this you want your object to implement INotifyPropertyChanged, and trigger a notification in the set method of a property.