Multiple DataBindings to the same control
-
I think I have come across a fairly common situation but I cant find any help with this. I have a simple form with 2 controls. A text box (Employee's Name) and a combo box (Employee's Job Title). I have a DataTable that I have tied both the text box and combo box to. Changing positions using the CurrencyManager works fine and everything is good. But now I want to populate the combo-box with a list of all available jobs. I want to be able to select a item from the list and have it update the appropriate row in my DataTable as would an edit within the text box. I have the following code, but I get un-expected results.
DataTable jobs = new DataTable("Jobs"); jobs.Columns.Add("Job", typeof(string)); jobs.Rows.Add(new object[] {"Software Engineer"}); jobs.Rows.Add(new object[] {"Project Manager"}); jobs.Rows.Add(new object[] {"Tester"}); jobs.Rows.Add(new object[] {"Documenter"}); jobs.Rows.Add(new object[] {"Project Lead"}); cboJob.DataSource = t; cboJob.DisplayMember = "Job"; DataTable namesAndJobs = new DataTable("NamesAndJobs"); namesAndJobs.Columns.Add("Name", typeof(string)); namesAndJobs.Columns.Add("Job", typeof(string)); namesAndJobs.Rows.Add(new object[] {"Bob", "Project Lead"}); namesAndJobs.Rows.Add(new object[] {"Mark", "Documenter"}); namesAndJobs.Rows.Add(new object[] {"Henry", "Project Manager"}); txtName.DataBindings.Add("Text", namesAndJobs, "Name"); cboJob.DataBindings.Add("Text", namesAndJobs, "Job");
Can what I want to do, even be done? If not, what is the purpose of allowing multiple DataBindings to the same control? "I am so smart. S-M-R-T. I mean, S-M-A-R-T" - Homer