C# EF,WinForms:如何将相关导航属性添加到DataGridView?

C# EF,WinForms:如何将相关导航属性添加到DataGridView?,c#,winforms,entity-framework,datagridview,ado.net,C#,Winforms,Entity Framework,Datagridview,Ado.net,我的数据库中有用户表和用户组表: 在本教程之后,我为用户生成了EF模型(首先是数据库)和数据源 在我的表单上,我创建了BindingSource(bsUsers)并将DataGridView绑定到它,因此它显示ID和用户名 以下是我在表单启动时加载数据的方式: _myDbContext = new MyDbContext(); _myDbContext.Users.Load(); bsUsers.DataSource = _myDbContext.Users.Local.ToBinding

我的数据库中有用户表和用户组表:

在本教程之后,我为用户生成了EF模型(首先是数据库)和数据源

在我的表单上,我创建了BindingSource(bsUsers)并将DataGridView绑定到它,因此它显示ID和用户名

以下是我在表单启动时加载数据的方式:

_myDbContext = new MyDbContext();

_myDbContext.Users.Load();

bsUsers.DataSource = _myDbContext.Users.Local.ToBindingList();
但是现在我想在相同的DataGridView中显示GroupName。最好的方法是什么

我试图在列
DataPropertyName
中指定
UserGroup.GroupName
,但它不起作用,单元格仍然为空

到目前为止,我找到的唯一解决方案是创建一个新的未绑定列并手动填充:

foreach (var item in (IList<User>)bsUsers.DataSource) 
{
    dgw.Rows[i].Cells["GroupName"].Value = item.UserGroup.Name; 
}
foreach(在(IList)bsUsers.DataSource中的var项)
{
dgw.Rows[i]。单元格[“GroupName”]。值=item.UserGroup.Name;
}

但这看起来不是一个好办法。例如,在更改用户组后,我需要再次更新它,或者在添加新记录时。

找到了一种更好的方法:(仅适用于只读,不适用于在DataGridView中编辑)

在列中,我将
DataPropertyName
设置为
UserGroup.GroupName

CellFormatting
事件中,我为所有绑定到嵌套属性(在
DataPropertyName
中具有
的属性)的列调用此方法以显示其值:

private void dgwUsers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    var dgw = (DataGridView) sender;
    DataGridViewColumn column = dgw.Columns[e.ColumnIndex];
    DataGridViewRow row = dgw.Rows[e.RowIndex];

    if (column.DataPropertyName.Contains("."))
    {
        e.Value = BindingHelper.GetPropertyValue(row.DataBoundItem, column.DataPropertyName);
    }
}
我还将ComboBox绑定到同一个BindingSource,该BindingSource允许更改当前所选用户的组。要更新相应DataGridView单元格中的值,请在组合框的
SelectedIndexChanged
事件中执行此操作:

private void cbbUserGroup_SelectedIndexChanged(object sender, EventArgs e)
{
    Validate();
    dgwUsers.UpdateCellValue(dgwUsers.Columns["GroupName"].Index, dgwUsers.CurrentRow.Index);
}

找到了更好的方法:(仅适用于只读,不适用于在DataGridView中编辑)

在列中,我将
DataPropertyName
设置为
UserGroup.GroupName

CellFormatting
事件中,我为所有绑定到嵌套属性(在
DataPropertyName
中具有
的属性)的列调用此方法以显示其值:

private void dgwUsers_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    var dgw = (DataGridView) sender;
    DataGridViewColumn column = dgw.Columns[e.ColumnIndex];
    DataGridViewRow row = dgw.Rows[e.RowIndex];

    if (column.DataPropertyName.Contains("."))
    {
        e.Value = BindingHelper.GetPropertyValue(row.DataBoundItem, column.DataPropertyName);
    }
}
我还将ComboBox绑定到同一个BindingSource,该BindingSource允许更改当前所选用户的组。要更新相应DataGridView单元格中的值,请在组合框的
SelectedIndexChanged
事件中执行此操作:

private void cbbUserGroup_SelectedIndexChanged(object sender, EventArgs e)
{
    Validate();
    dgwUsers.UpdateCellValue(dgwUsers.Columns["GroupName"].Index, dgwUsers.CurrentRow.Index);
}

您只需在模型中重写ToString()。尽管我强烈建议使用ViewModel

public class UserGroup{

public int Id { get; set; }
public string GroupName { get; set; }

public override string ToString() {

    return GroupName;
}

您只需在模型中重写ToString()。尽管我强烈建议使用ViewModel

public class UserGroup{

public int Id { get; set; }
public string GroupName { get; set; }

public override string ToString() {

    return GroupName;
}

你好如何将排序逻辑添加到嵌套列中?我同意@andrew0007的说法:虽然这非常有效,但它不允许datagridview使用BindingHelper对列进行排序。嗨!如何将排序逻辑添加到嵌套列中?我同意@andrew0007的说法:虽然这非常有效,但它不允许datagridview使用BindingHelper对列进行排序。