简体   繁体   English

C#数据绑定ListBox

[英]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 ): 我知道如何使用DataSet设计器Employee表添加为细节(多重绑定控件) Form1 (Windows窗体应用程序 ):

Dragging the Company database's Employee table (as details) to form creates the following components: 拖动Company数据库的Employee表(作为详细信息)以形成以下组件:

• CompanyDataSet •CompanyDataSet
• employeeBindingSource •employeeBindingSource
• employeeTableAdapter •employeeTableAdapter
• tableAdapterManager •tableAdapterManager
• employeeBindingNavigator •employeeBindingNavigator

  1. How do you add a ListBox to Form1 that displays the ProjectDescription of all the projects the currently displayed Employee manages? 如何将一个列表框添加到Form1,以显示当前显示的Employee管理的所有项目的ProjectDescription? What ListBox properties need to be set? 需要设置哪些ListBox属性?

    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 参数将为employeeIDtextbox.Text

  2. Project records will be created programmatically (OleDbConnection, OleDbCommand) from Form2. 项目记录将从Form2中以编程方式创建(OleDbConnection,OleDbCommand)。 What statement is required to update Form1 when it is displayed? 显示Form1时需要什么语句来更新它?

I have searched the web and read pages and pages of Microsoft documentation and have hit a wall... 我在网上搜索并阅读了Microsoft文档的页面和页面,并且碰壁了...

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 如果您要询问如何将ProjectDescriptions放入列表框,则可以简单地遍历查询返回的记录集,并将每个条目添加到列表框中

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. 我不能保证代码确实能做到,但是我没有C#IDE来与我进行测试。 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. 要在从Form2输入数据后更新Form1 ,请将代码放入Form1.Activated ,以检查更新的数据并适当地更改Form1对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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