views:

88

answers:

1

Hi, I have a class

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 !

+4  A: 

You are referring to a Data**Property**Name, 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.

Yannick M.
I thought the fields had auto-implemented properties ?!! .. As inpublic string Var;is equivalent to public string Var {get; set;}..I may be wrong, but whats the difference between those two cases functionally ? (Unless the bindingList is an example of what doesnt work for fields and I just made a fool out of myself :) )
IM
It's the other way around. Auto properties have compiler created getter and setter methods that use private fields.
Yannick M.
And the DataGridTextBoxColumns DataPropertyName uses reflection to get the value of the Property you supplied. As the name suggests.
Yannick M.
@IM: properties and fields have a number of differences conceptually (e.g. field is always a memory location and thus can be passed to `out` or `ref` argument, or have its address taken with ` this is not true for property), but more important is that fields and properties are completely different when accessed via Reflection, so any code that uses the latter either has to support only one, or have two code paths to support both. Data binding works via Reflection, and only supports properties.
Pavel Minaev
wow..learnt something new today, fantastic ! btw guys -- what books do you guys consider thebest for learnign C# Winforms and WPF.. I'm asking coz my Microsoft Press C# 2005 book had no mention of this difference between fields and properties, and you guys obviously know better.Thanks !
IM
Thx guys.. that was it.. btw.. Yannick.. on the INotifyPropertyChanged.. I thought the difference between a regular List and a BindingList was that with a regular list I'd have to explicitly do a NotifyPropertyChanged like you siad, whereas with the BindingList I would NOT have to as it was taken care of by the platform. Src : http://www.switchonthecode.com/tutorials/csharp-tutorial-binding-a-datagridview-to-a-collection
IM
The BindingList will only notify subscribers that the content of the list has changed (an element added, removed), not the property value of a list item.
Yannick M.