Assigning List<T> to a DataGridView.DataSource - data seems to be there, but grid is blank

If you look at the screenshot below, I've got the code stopped just after the assignment of the List<T> to the DataGridView.DataSource.

The DataSource in the Watch window shows a count of 95 items, and if I expand that list, each item has the correct data.

What am I missing?  : /

datasource
MemParts:

public class MemParts
    {       
        public MemParts() { }

        public void InitMemPartsAsPreview()
        {
            CustID = -1;
            PartNumber = "";
            Description = "";
            TypeID = GlobalDatabaseHelperMethods.GetCellInfo("PartTypes", "PartTypesID", "where Label = 'RAW'", "int").ToString().ExToInt32();
            UMID = GlobalDatabaseHelperMethods.GetCellInfo("UnitMeasureTypes", "UnitMeasureID", "where Label = 'EA'", "int").ToString().ExToInt32();
            ClassID = -1;
            NumberOfLeads = -1;
            CommmonStock = false;
            RoHS = true;
            AllowCrossesID = GlobalDatabaseHelperMethods.GetCellInfo("AllowCrossesTypes", "AllowCrossesID", "where Label = 'APPROVE'", "int").ToString().ExToInt32();
            PackageReqID = -1;
            StatusID = -1;
            Notes = "";
            HasSafetyStock = false;
            ReorderTrigger = 0.00;
            ReorderQnty = 0.00D;
            OtherNotes = "";
        }

        public int CustID = -1;
        public string PartNumber = "";
        public string Description = "";
        public int TypeID = -1;
        public int UMID = -1;
        public int ClassID = -1;
        public int NumberOfLeads = -1;
        public bool CommmonStock = false;
        public bool RoHS = true;
        public int AllowCrossesID = -1;
        public int PackageReqID = -1;
        public int StatusID = -1;
        public string Notes = "";
        public bool HasSafetyStock = false;
        public double ReorderQnty = 0.00D;
        public string OtherNotes = "";
        public double ReorderTrigger = 0.00;
    }//end of class

Open in new window

LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
Hi Tom;

With the solution I posted I did not keep the initial values as you had it so you can do that either in the constructor or you will need to change the property code even more and create private fields for each of the properties.
0
 
Fernando SotoRetiredCommented:
The issue I believe you are having is that the DataSource of the DataGridView uses the public property of the class and NOT the fields you added. Try the following changes
//  Change this line
public int CustID = -1;

//  To this
public int CustID { get; set; }

Open in new window

Do the same for all the others that follow it.
1
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
Also are the columns auto generated or are they created before hand?  If they are created before hand did you set the DataPropertyName
0
 
Tom KnowltonWeb developerAuthor Commented:
There was no issue with auto-generated columns, but something to keep in mind.

Creating properties for the fields did the trick, thank you!


[code]

public class MemParts
    {      
        public MemParts() { }

        public MemParts(int custID, string partNumber, string description, int typeID, int umid, int classID, int numberOfLeads, bool commmonStock, bool rohs, int allowCrossesID, int packageReqID, int statusID, string notes, bool hasSafetyStock, double reorderQnty, string otherNotes, double reorderTrigger)
        {
            CustID = custID;
            PartNumber = partNumber;
            Description = description;
            TypeID = typeID;
            UMID = umid;
            ClassID = classID;
            NumberOfLeads = numberOfLeads;
            CommmonStock = commmonStock;
            RoHS = rohs;
            AllowCrossesID = allowCrossesID;
            PackageReqID = packageReqID;
            StatusID = statusID;
            Notes = notes;
            HasSafetyStock = hasSafetyStock;
            ReorderQnty = reorderQnty;
            OtherNotes = otherNotes;
            ReorderTrigger = reorderTrigger;
        }

        public void InitMemPartsAsPreview()
        {
            CustID = -1;
            PartNumber = "";
            Description = "";
            TypeID = GlobalDatabaseHelperMethods.GetCellInfo("PartTypes", "PartTypesID", "where Label  = 'RAW'", "int").ToString().ExToInt32();
            UMID = GlobalDatabaseHelperMethods.GetCellInfo("UnitMeasureTypes", "UnitMeasureID", "where Label  = 'EA'", "int").ToString().ExToInt32();
            ClassID = -1;
            NumberOfLeads = -1;
            CommmonStock = false;
            RoHS = true;
            AllowCrossesID = GlobalDatabaseHelperMethods.GetCellInfo("AllowCrossesTypes", "AllowCrossesID", "where Label  = 'APPROVE'", "int").ToString().ExToInt32();
            PackageReqID = -1;
            StatusID = -1;
            Notes = "";
            HasSafetyStock = false;
            ReorderTrigger = 0.00;
            ReorderQnty = 0.00D;
            OtherNotes = "";
        }

            private int _custid = -1;
            private string _partnumber = "";
            private string _description = "";
            private int _typeid = -1;
            private int _umid = -1;
            private int _classid = -1;
            private int _numberofleads = -1;
            private bool _commmonstock = false;
            private bool _rohs = true;
            private int _allowcrossesid = -1;
            private int _packagereqid = -1;
            private int _statusid = -1;
            private string _notes = "";
            private bool _hassafetystock = false;
            private double _reorderqnty = 0.00D;
            private string _othernotes = "";
            private double _reordertrigger = 0.00D;

        public int CustID
        {
            get
            {
                return _custid;
            }

            set
            {
                _custid = value;
            }
        }

        public string PartNumber
        {
            get
            {
                return _partnumber;
            }

            set
            {
                _partnumber = value;
            }
        }

        public string Description
        {
            get
            {
                return _description;
            }

            set
            {
                _description = value;
            }
        }

        public int TypeID
        {
            get
            {
                return _typeid;
            }

            set
            {
                _typeid = value;
            }
        }

        public int UMID
        {
            get
            {
                return _umid;
            }

            set
            {
                _umid = value;
            }
        }

        public int ClassID
        {
            get
            {
                return _classid;
            }

            set
            {
                _classid = value;
            }
        }

        public int NumberOfLeads
        {
            get
            {
                return _numberofleads;
            }

            set
            {
                _numberofleads = value;
            }
        }

        public bool CommmonStock
        {
            get
            {
                return _commmonstock;
            }

            set
            {
                _commmonstock = value;
            }
        }

        public bool RoHS
        {
            get
            {
                return _rohs;
            }

            set
            {
                _rohs = value;
            }
        }

        public int AllowCrossesID
        {
            get
            {
                return _allowcrossesid;
            }

            set
            {
                _allowcrossesid = value;
            }
        }

        public int PackageReqID
        {
            get
            {
                return _packagereqid;
            }

            set
            {
                _packagereqid = value;
            }
        }

        public int StatusID
        {
            get
            {
                return _statusid;
            }

            set
            {
                _statusid = value;
            }
        }

        public string Notes
        {
            get
            {
                return _notes;
            }

            set
            {
                _notes = value;
            }
        }

        public bool HasSafetyStock
        {
            get
            {
                return _hassafetystock;
            }

            set
            {
                _hassafetystock = value;
            }
        }

        public double ReorderQnty
        {
            get
            {
                return _reorderqnty;
            }

            set
            {
                _reorderqnty = value;
            }
        }

        public string OtherNotes
        {
            get
            {
                return _othernotes;
            }

            set
            {
                _othernotes = value;
            }
        }

        public double ReorderTrigger
        {
            get
            {
                return _reordertrigger;
            }

            set
            {
                _reordertrigger = value;
            }
        }
    }//end of class

[/code]
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Just a note you can do the shorthand if you're not implementing custom logic.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.