C# DatagridView with List<t> as DataSource

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ciri
    New Member
    • Oct 2007
    • 18

    C# DatagridView with List<t> as DataSource

    Hi all, I've a problem with a datagridview.
    The data source of the control is a List<t> where t is type of a class I've created.
    The class (Person) has some properties like Name, Surname and so on.
    Now my datagridview is bounded to a collection of Person:

    dgwPersons=new DataGridView();
    dgwPersons.Auto GenerateColumns =false;
    dgwPersons.Colu mns.Add("Name", "Name");
    dgwPersons.Colu mns[0].DataPropertyNa me="Name";
    dgwPersons.Colu mns.Add("Surnam e","Surname" );
    dgwPersons.Colu mns[1].DataPropertyNa me="Surname";
    BindingSource personsBindingS ource=new BindingSource() ;
    personsBindingS ource.DataSourc e=Persons; //Persons is the collection
    dgwPersons.Data Source=personBi ndingSource;

    Most of properties of the Person class are strings (like Name and Surname), so I had no problem to bound them to the dgw.
    I can even edit the fields in the dgw and have the corresponding property in memory changed.

    Now the question is that the class Person have a property (job) of the type of Job (another class);
    Furthermore I've got another list<t> where t is type of Job.
    The collection contains all possible jobs.
    I'd like to have a combobox column in my dgw that shows the corrent job of the person and gives the user the chance to choiche between all jobs in the jobs collection.

    How can I do that?

    Thank you very much guys.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Create a custom combobox column, set the default datasource for the combobox to me your jobs list?

    Comment

    • Ciri
      New Member
      • Oct 2007
      • 18

      #3
      Originally posted by Plater
      Create a custom combobox column, set the default datasource for the combobox to me your jobs list?
      Ok. I'm trying:
      DataGridViewCol umn dgwCbClm=new DataGridViewCom boBoxColumn();

      How do I set now the default datasource for the combobox?

      Thanks again.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        The property is hidden in intellisence, but according to msdn you can still do this:
        Code:
        DataGridViewComboBoxColumn d = new DataGridViewComboBoxColumn();
        d.DataSource = myComboDataSource;

        Comment

        • Ciri
          New Member
          • Oct 2007
          • 18

          #5
          Originally posted by Plater
          The property is hidden in intellisence, but according to msdn you can still do this:
          Code:
          DataGridViewComboBoxColumn d = new DataGridViewComboBoxColumn();
          d.DataSource = myComboDataSource;
          Great, it works!
          The problem now is that it shows "WindowsApplica tion.Job".
          How do I associate the combobox to the effective value of Job property of related person?
          And how do I choose what of the property of the job to show? (example jobName, or jobTime...)

          Thanks.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            There are two properties you can use:
            Code:
            d.ValueMember = "JobName";
            d.DisplayMember="JobName"
            One should dictate which property is returned as the 'selected value' and which property is used for display purposes

            There is also this setting that I am not sure if you need:
            //d.DataPropertyN ame

            And somewhere I saw a place where you could tell it which value is the "default" to select from, which should allow you to auto-select the current "job" in the person object.

            Comment

            • CFQüeb
              New Member
              • Feb 2008
              • 2

              #7
              Excellent!
              Nice tip...

              Working here!

              Comment

              • Ciri
                New Member
                • Oct 2007
                • 18

                #8
                Originally posted by CFQüeb
                Excellent!
                Nice tip...

                Working here!
                Perfect! It works.

                I just had to add a .Find delegate in the set method of the JobName property
                to allow the user to change the instance of Job via the dropdownlist.
                I just wrote:

                Code:
                string JobName
                {
                    get{return _job.Name}
                    set
                    {
                            Job newJob=
                                Jobs.Find(delegate(Job myNewJob){return 
                (myNewJob.Name==value);});
                            if (newJob!=null)
                                _job=newJob;
                    }
                }
                Thanks again.
                Last edited by Plater; Feb 8 '08, 04:59 PM. Reason: added [CODE] tags

                Comment

                Working...