4 Replies - 1771 Views - Last Post: 03 April 2013 - 11:17 AM Rate Topic: -----

#1 thedonutbandit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 03-April 13

Not all items appearing in DataGridView

Posted 03 April 2013 - 09:25 AM

Hi guys,

I am having a problem with all the items in my list not appearing in the datagridview. The only ones that appear in columns of datagridview1 are firstname, lastname, empId, phone, and ssn but department, supervisor, and position do not appear. in form 1, the user enters employee info that then populates in the datagridview when they click the add button. the display button opens form2 and this displays the employees in the datagridview. like i said though, position, supervisor, and department do not appear in the datagridview. take a look at my code and let me know what I am doing wrong? thanks!

Employees Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WindowsFormsApplication1;

namespace WindowsFormsApplication1
{
    public abstract class Employee
    {
        // class properties
        public virtual string firstName { get; set; }
        public virtual string lastName { get; set; }
        public virtual string empID { get; set; }
        public virtual string phone { get; set; }
        public virtual string sSn { get; set; }

        public Employee(string fName, string lName, string id,
            string phn, string sn)
        {
            firstName = fName;
            lastName = lName;
            empID = id;
            phone = phn;
            sSn = sn;
        }
    }
}



managers class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace WindowsFormsApplication1
{
    public class Managers : Employee, INotifyPropertyChanged
    {
        private string _department;
        private string _position;
        private string _supervisor;
        private string _firstname;
        private string _lastname;
        private string _id;
        private string _phn;
        private string _sn;

        public Managers(string fName, string lName, string id, string phn,
            string sn, string dept, string pos, string sup)
            : base(fName, lName,
                id, phn, sn)
        {
            department = dept;
            supervisor = sup;
            position = pos;
        }

        public override string firstName
        {
            get
            {
                return _firstname;
            }
            set
            {
                if (value != this._firstname)
                {
                    this._firstname = value;
                    NotifyPropertyChanged("firstName");
                }
            }
        }

        public override string lastName
        {
            get
            {
                return _lastname;
            }
            set
            {
                if (value != this._lastname)
                {
                    this._lastname = value;
                    NotifyPropertyChanged("lastName");
                }
            }
        }

        public override string empID
        {
            get
            {
                return _id;
            }
            set
            {
                if (value != this._id)
                {
                    this._id = value;
                    NotifyPropertyChanged("empID");
                }
            }
        }

        public override string phone
        {
            get
            {
                return _phn;
            }
            set
            {
                if (value != this._phn)
                {
                    this._phn = value;
                    NotifyPropertyChanged("phone");
                }
            }
        }

        public override string sSn
        {
            get
            {
                return _sn;
            }
            set
            {
                if (value != this._sn)
                {
                    this._sn = value;
                    NotifyPropertyChanged("ssn");
                }
            }
        }

        public string department
        {
            get
            {
                return _department;
            }
            set
            {

                if (value != this._department)
                {
                    this._department = value;
                    NotifyPropertyChanged("department");
                }

            }
        }

        public string supervisor
        {
            get
            {
                return _supervisor;
            }
            set
            {

                if (value != this._supervisor)
                {
                    this._supervisor = value;
                    NotifyPropertyChanged("supervisor");
                }

            }
        }

        public string position
        {
            get
            {
                return _position;
            }
            set
            {

                if (value != this._position)
                {
                    this._position = value;
                    NotifyPropertyChanged("position");
                }

            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }

        #endregion
    }
}


form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public static BindingList<Employee> list = new BindingList<Employee>();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string firstName = textBox1.Text;
            string lastName = textBox2.Text;
            string empID = textBox3.Text;
            string SSN = textBox4.Text;
            string phone = textBox5.Text;
            string department = textBox6.Text;
            string supervisor = textBox7.Text;
            string position = textBox8.Text;

           list.Add(new Managers(firstName, lastName, empID, SSN, phone, department, supervisor, position));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //show the form 2
            Form2 form2 = new Form2();
            form2.ShowDialog();//enforces user to not back to form1 unless finishing work on form2
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}


form 2, this might be where the problem is?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

            dataGridView1.DataSource = Form1.list; 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Not all items appearing in DataGridView

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5524
  • View blog
  • Posts: 11,834
  • Joined: 02-June 10

Re: Not all items appearing in DataGridView

Posted 03 April 2013 - 09:58 AM

You at least need to do some basic debugging. We're not going to comb through 300+ lines of your code and do it all for you. - Espeically with horrific and meaningless names like button1 that don't tell us a damned thing.

If something is not happening, then place breakpoints to see if the method is even being called. If you are replacing rather than appending... etc.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Feb 2013
Spoiler



This post has been edited by tlhIn`toq: 03 April 2013 - 09:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5524
  • View blog
  • Posts: 11,834
  • Joined: 02-June 10

Re: Not all items appearing in DataGridView

Posted 03 April 2013 - 10:06 AM

Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: dnt no wut i m do-n, coz, al gud, b4, ny1, sum1, u r, and so on like this guy. Its completely disrespectful to the senior coding professionals that volunteer here to mentor you.

    Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 thedonutbandit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 03-April 13

Re: Not all items appearing in DataGridView

Posted 03 April 2013 - 10:49 AM

View PosttlhIn`toq, on 03 April 2013 - 10:06 AM, said:

Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.



  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.
    Spoiler


  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    Spoiler

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.


  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: dnt no wut i m do-n, coz, al gud, b4, ny1, sum1, u r, and so on like this guy. Its completely disrespectful to the senior coding professionals that volunteer here to mentor you.

    Spoiler


Thanks, these are very helpful guidelines for writing code. This is a challenging course I am currently in and these guidelines will surely help when it comes to the quality of my work.
Was This Post Helpful? 0
  • +
  • -

#5 thedonutbandit  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 03-April 13

Re: Not all items appearing in DataGridView

Posted 03 April 2013 - 11:17 AM

I was able to find out why those three elements were not appearing in my datagridview1. I was referencing the wrong class in my bindinglist:
[code}
public static BindingList<Managers> list = new BindingList<Managers>();
[/code]
before I had
public static BindingList<Employees> list = new BindingList<Employees>();


Once I changed that in m bindinglist, the correct elements appeared in my datagridview.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1