简体   繁体   中英

C# data bound ListBox

I have the following two tables:

Employee table:

EmployeeID  
Name

etc.

Project table:

ProjectID  
EmployeeID  
ProjectDescription

Each Employee can manage zero or more projects.

I know how to use the DataSet Designer to add the Employee table as details (multiple bound controls) on Form1 ( Windows Form Application ):

Dragging the Company database's Employee table (as details) to form creates the following components:

• CompanyDataSet
• employeeBindingSource
• employeeTableAdapter
• tableAdapterManager
• employeeBindingNavigator

  1. How do you add a ListBox to Form1 that displays the ProjectDescription of all the projects the currently displayed Employee manages? What ListBox properties need to be set?

    I believe I will need the following query:

     SELECT Project.ProjectDescription FROM Project RIGHT JOIN Employee ON Project.EmployeeID = Employee.EmployeeID WHERE EmployeeID = ? 

    Parameter would be the employeeIDtextbox.Text

  2. Project records will be created programmatically (OleDbConnection, OleDbCommand) from Form2. What statement is required to update Form1 when it is displayed?

I have searched the web and read pages and pages of Microsoft documentation and have hit a wall...

If you're asking how to get the ProjectDescriptions into the ListBox, you could simply iterate through the recordset returned by the query and add each entry to the listbox

foreach (DataRow r in foo)
{
    projectlistbox.Items.Add(r["ProjectDescription"]);
}

it's inelegant but it works. I can't promise that code does exactly though, I don't have a C# IDE to test it against with me. It should point you in the right direction though.

To update Form1 after data has been entered from Form2 , put code into Form1.Activated that checks for the updated data and changes objects on Form1 appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM