Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

.Net databinding question

Options
  • 11-11-2004 11:45am
    #1
    Registered Users Posts: 7,468 ✭✭✭


    This relates to System.Windows.Forms, in particular comboboxes but the theory applies accross databound controls. Excuse the code, its only for illustrative purposes (assume its correct). cboProjects is a Combobox and has to be DataBound.
    class ProjectDetails
    {
         private DataTable _dtProjects;
         
         private void PopulateProjectDropDown()
         {
                     // GetProjects() returns a datatable
    		 _dtProjects = projectControl.GetProjects(); 
    		 this.cboProjects.DataSource = _dtProjects; 
    		 this.cboProjects.DisplayMember = "Project Name";
    		 this.cboProjects.ValueMember = "ProjectID";
         }
    }
    

    You get the idea. Now you now have two DataTable objects in memory storing the same thing; _dtProjects and cboProjects.DataSource. Reason being that properties are always passed by value.

    Which is more effiecient: use two objects or just use the DataSource property on the combobox? I'd say using the comboboxes DataSource property but I need to hear any arguements against. Theres going to be a disagreement, y'see.

    So you'd have something like this
         private void PopulateProjectDropDown()
         {
    		  // this method returns a datatable
    		 this.cboProjects.DataSource = projectControl.GetProjects();
    		 this.cboProjects.DisplayMember = "Project Name";
    		 this.cboProjects.ValueMember = "ProjectID;
         }
    

    and then whenever you need to use the dataset you'd be using cboProject.DataSource.


Comments

  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Evil Phil wrote:
    Now you now have two DataTable objects in memory storing the same thing; _dtProjects and cboProjects.DataSource.

    Do you not have two DataTable objects referencing the same data-store (i.e. its only a shallow copy)? I can't remember (cause I don't data-bind that often, and when I do, I more often use ILists then DataTables) ... but if you have a DataTable as a datasource and add /remove a record to the DataTable (_dtProjects above), do you need to rebind the combo in order to see the change? If not, then I don't think you have two seperate copies of the data. I think its just a shallow-copy which is done. But I could be wrong.
    I'd say using the comboboxes DataSource property but I need to hear any arguments against.
    DataSource is defined as type object, so you'll have to cast back to type DataTable every time you want to use it...which strikes me as messy.

    Other than memory efficiency (which will depend on whether or not the data is actually copied), I can't see much else between them.

    jc


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Yeah you're right it is a shallow copy, I don't need to rebind. So I was wrong, thanks bonkey.


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    No probs.


Advertisement