我有一个C#,Windows窗体应用程序。一种形式是为所有帐户维护页面。它是基于在表中的单个表和导航结合使用,以允许用户通过帐户列表进行浏览。每个帐户只有一个地址。现在我想改变它,以便每个帐号可以有多个地址。我有一个新的表来存储地址和组合表到帐户匹配的地址。上的形式,我添加了一个数据网格,并增加了地址字段(地址类的列表)到帐户类。我想要做的是基础帐户的地址字段的新数据网格。我想不出确切的语法。有人可以帮助我弄清楚如何将新的数据网格绑定到地址列表?谢谢!
所以,我的代码看起来是这样的(我已经简化了数据,使其更易于包括):
// This is how I originally bound my data source to the navigator on the form
List<Accounts> accounts = accountMaintenanceBLL.GetAccounts();
accountMaintenanceBindingSource.DataSource = accounts;
// Here's the original Account class; the addresses field is new to handle the multiple addresses
public class Account
{
public int ID { get; set; }
public string name { get; set; }
public string account { get; set; }
public List<Address> addresses { get; set; }
}
// Here's what the new address class looks like
public class Address
{
public int AddressID {
public string Address { get; set; }
public string City { get; set; }
}
我想一个新的数据网格绑定到地址字段中的帐户。我已经尝试了一些东西,但基本上我想要做这样的事情:
this.dataGridView1.DataSource = this.addressBindingSource;
addressBindingSource.DataSource = ((Address)itemMaintenanceViewModelBindingSource.Current).addresses;
这不工作,我不能完全弄清楚如何做到这一点。
下面是数据的表示:
Account Table
----------------------------
| ID | Name | Account |
-----------------------------
| 123 | Acme | 98765 |
-----------------------------
| 124 | Binford| 34566 |
-----------------------------
| 125 | CoTech | 32232 |
-----------------------------
Address Table
----------------------------------
| Address | Address | City |
| ID | | |
---------------------------------|
| 1 | 123 Main | Detroit|
----------------------------------
| 2 | 13 Elm | Denver |
----------------------------------
| 3 | 90 Rodeo | LA |
----------------------------------
| 4 | 57 Park | NYC |
----------------------------------
AccountAddress Table
-----------------------------
| Comp. ID | Address ID |
-----------------------------
| 123 | 1 |
-----------------------------
| 124 | 2 |
-----------------------------
| 125 | 3 |
-----------------------------
| 125 | 4 |
-----------------------------
下面是在顶部的结合导航器,文本框与所述数据的帐户信息,然后示出了用于该帐户的地址的网格的形式的表示。
itemMaintenanceViewModelBindingNavigator
ID: 125 Name: Binford Account: 34566
Addresses:
dataGridView1
------------------
| 90 Rodeo | LA |
------------------
| 57 Park | NYC |
------------------
我希望这是正确的。从我可以破译,这似乎是类似于“主从式”的局面。这是一个有点混乱,为什么显示的数据看起来是DataTable
s,但该代码使用List<T>
作为DataSource
到网格?
下面使用List<Account>
作为DataSource
到BindingSource
。正如你所指出的,自从新的“地址”字段现在是一个“集”,而不是一个单一类型...网格将“忽略”此属性。这是可能的,创建一个将返回一个“单一”字符串换行符与所有不同的地址,然后在网格中的“多”单元格中显示这个新的“财产”(而不是方法)。或使用您的建议,即遵循主详细的想法是创建一个第二DataGridView
显示多个地址。这种网格内容将是“动态的”,当选择在“账户”格变化都会改变。
使用Account
类,张贴在的问题,并使用BindingSource
,如张贴代码...
List<Accounts> accounts = accountMaintenanceBLL.GetAccounts();
accountMaintenanceBindingSource.DataSource = accounts;
然后,我们用这个作为一个DataSource
到“帐户”网格。
dgvAccounts.DataSource = accountMaintenanceBindingSource;
下一步是设置“地址”网格DataSource
。由于该网格的数据源是“动态”,并会改变的时候了“户口”网格“选择”的变化......这个变量将需要暴露在“账户”网格“选择改变”事件。这是用来“改变”的“地址”网格数据源的事件。我猜还有其他的方式来实现这一点,可能需要微调您的环境。
在这种情况下,“全局” BindingSource
AddressBindingSource
变量用于“地址”网格数据源。
BindingSource AddressBindingSource = new BindingSource();
的形式加载事件可能类似于下面...
private void Form1_Load(object sender, EventArgs e) {
List<Account> accounts = GetAccounts();
accountMaintenanceBindingSource.DataSource = accounts;
dgvAccounts.DataSource = accountMaintenanceBindingSource;
dgvAddresses.DataSource = AddressBindingSource;
}
最后是布线了“帐户”电网SelectionChanged
事件改变“地址”网格数据源到当前选择“帐户” ......它可能类似于下面...
private void dataGridView1_SelectionChanged(object sender, EventArgs e) {
int rowIndex = dgvAccounts.CurrentCell.RowIndex;
Account acct = (Account)dgvAccounts.Rows[rowIndex].DataBoundItem;
AddressBindingSource.DataSource = acct.Addresses;
}
以上,我们得到了“当前单元格”行的行索引,那么该行被映射到accounts
列表中选择合适的“帐户”的对象。最后的“地址”绑定源设置为地址是“帐户”列表中。
我希望这有帮助。