I have a SQL Database where two tables have PK Relation. tblProject.contractorID is Foreign Key column and tblCompany.contractorID is Primary Key column.

sql

I have created a Windows Form, marked tblProject.contractorID as ComboBox, marked tblProject as Details and drag and drop tblProject to the Form. Now, I have labels, textbox and combobox of the tblProject and a related binding navigator. (ihaleDataSet, tblProjectBindingSource, tblProjectTableAdapter, tableAdapterManager and tblProjectBindingNavigator)

2 When I run the project and click contractorID ComboBox, I want to Display tblCompany.shortName values. After selecting one of that values, I want to write tblCompany.contractorID value to tblProject.ContractorID and record accordingly.

I tried Data Binding Mode (DataSource= tblCompanyBindingSource, DisplayMember = "shortName", ValueMember = "companyID")

enter image description here

        // 
        // contractorIDComboBox
        // 
        this.contractorIDComboBox.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.tblProjectBindingSource, "contractorID", true));
        this.contractorIDComboBox.DataSource = this.tblCompanyBindingSource;
        this.contractorIDComboBox.DisplayMember = "shortName";
        this.contractorIDComboBox.FormattingEnabled = true;
        this.contractorIDComboBox.Location = new System.Drawing.Point(181, 100);
        this.contractorIDComboBox.Name = "contractorIDComboBox";
        this.contractorIDComboBox.Size = new System.Drawing.Size(121, 21);
        this.contractorIDComboBox.TabIndex = 8;
        this.contractorIDComboBox.ValueMember = "companyID";

It does not work. What shall I do to accomplish my aim?

有帮助吗?

解决方案

I' ve solved the problem. Just replacing "Text" with "SelectedValue" in the first row is enough.

More information is here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top