Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > some of my class<T>'s members don't turn up on datagridview bound to BindingList<T>
 

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

Hi, I have a class

public classANote{
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 { returnreturn NoteCollectionDate.toString("hh/mm tt"); }
}
public DateTime ADate = DateTime.Now;
public double AAmount = 0.0D;
public string AName = "";
}

And a BindingList<ANote> 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 !


IMx  Thursday, September 17, 2009 7:33 PM
Yes..they do..
hey..I managed to solve the problem.. the problem was that I was using fields instead of properties for the ADate, AAMount and AName. The binding uses Reflection so they need to be properties..
As soon as I added the Auto-Implemented getters setters.. like thus:

public
DateTime ADate {get; set; }

public double
AAmount {get; set; }
public string AName {get; set; }


everything started working !

Thx for ur help !
  • Marked As Answer byIMx Friday, September 18, 2009 8:06 PM
  •  
IMx  Friday, September 18, 2009 8:06 PM

Ignore the double return typo inside the 2nd getter.. typo..

IMx  Thursday, September 17, 2009 7:34 PM
If you hover over the aList variable here:

grdNotes.DataSource = aList;
Do you see all of the values you expect? Are the names of those values that same you have set as the column DataPropertyNames?

Hope this helps.

www.insteptech.com ; msmvps.com/blogs/deborahk
We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
DeborahK  Friday, September 18, 2009 2:53 AM
Hi,
try out

grdNotes.DataSource = aList.ToArray();
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Friday, September 18, 2009 4:07 AM
@Gnanadurai :
Thx buddy for the suggestion .. but I guess you missed a part of my question .. I am using a BindingList and need to use that for the databinding properties.. using .toArray dpes not solve the problem..in fact it makes it worse as now the missing columns still turn don'tup and on top of that changes in the ANote class will not be automatically reflected int he form !
IMx  Friday, September 18, 2009 8:02 PM
Yes..they do..
hey..I managed to solve the problem.. the problem was that I was using fields instead of properties for the ADate, AAMount and AName. The binding uses Reflection so they need to be properties..
As soon as I added the Auto-Implemented getters setters.. like thus:

public
DateTime ADate {get; set; }

public double
AAmount {get; set; }
public string AName {get; set; }


everything started working !

Thx for ur help !
  • Marked As Answer byIMx Friday, September 18, 2009 8:06 PM
  •  
IMx  Friday, September 18, 2009 8:06 PM

You can use google to search for other answers

Custom Search

More Threads

• dataerror event is not raised
• ** VB.Net & Joined Tables **
• Set SelectedItem on DataGridViewComboBoxColumn When DataSource is Set
• How to set the property of the form as Modal
• Thank You
• Update database
• Dataset problem
• Grouping and formatting the columns in datagridview
• multiple bindingsource filters
• DataGridViewComboBoxCell conditional dropdown displaying ValueMember not Displaymember