如何在GridView中显示外键值

本文关键字:显示 键值 GridView | 更新日期: 2023-09-27 17:50:53

. NET应用程序中,我有一个连接两个表的存储过程,如下所示:

CREATE PROCEDURE dbo.usp_DepartmentsServiceChannelsSelect
AS
    SET NOCOUNT ON
SELECT     d.ID, d.Description,  s.ServiceChannel
FROM         Departments d
INNER JOIN [ServiceChannels] s
ON s.ID = d.ServiceChannel
GO

因此,使用ID、Description和连接到部门的ServiceChannel来选择部门。

我在一个类型化数据集中使用这个存储过程,它的TableAdapter叫做Department,它的方法是GetDepartmentsWithServiceChannels:

static public DepartmentDataTable GetDepartmentsWithServiceChannels()
{
    using (DepartmentTableAdapter departmentTA = new DepartmentTableAdapter())
    {
        return departmentTA.GetDepartmentsWithServiceChannels();
    }
}

我在视图中使用此方法在代码隐藏中绑定DepartmentCollection,如下所示:

    private void BindDepartmentsAfterSorting(string sortexpression, SortDirection 
              sortDirection)
    {
        DepartmentCollection deptCollection = 
              ServiceInterfaceRegistry.DepartmentManager.GetDepartments(false);
        if (deptCollection != null)
        {
            Common.Comparer<Department> objcmp = new Common.Comparer<Department>();
            objcmp.SortClasses.Add(new SortClass(sortexpression, sortDirection));
            deptCollection.Sort(objcmp);
        }
        MyGridView.DataSource = deptCollection;
        MyGridView.DataBind();
    }

其中MyGridvIew如下:

<asp:GridView ID="MyGridView" runat="server" DataKeyNames="ID" AutoGenerateColumns="False" Width="1000px"
            AllowSorting="True" AllowPaging="True" EmptyDataText="Geen afdeling gevonden." OnRowDataBound="AfdelingGridView_RowDataBound" OnRowDeleting="AfdelingGridView_RowDeleting"  OnRowEditing="MyGridView_RowEditing" OnPageIndexChanging="AfdelingGridView_PageIndexChanging" OnSorting="AfdelingGridView_Sorting">
            <Columns>
                <asp:ButtonField ButtonType="Button" Text="Delete/edit" CommandName="Edit">
                    <ItemStyle Width="20px" />
                </asp:ButtonField>
                <asp:TemplateField Visible ="False">
                    <ItemTemplate>
                        <asp:Button ID="Delete" runat="server" CommandName="Delete"  
           Text="Verwijderen" Font-Bold="false" />
                        </ItemTemplate> 
                        <ItemStyle Width="20px" />  
                  </asp:TemplateField>
                <asp:BoundField DataField="Description" HeaderText="Description" ReadOnly="True" SortExpression="Description" />
                <asp:BoundField DataField="ServiceChannel" HeaderText="Service Channel" ReadOnly="True"  />
            </Columns>
            <HeaderStyle HorizontalAlign="Left" />
</asp:GridView>

不幸的是,return departmentTA.GetDepartmentsWithServiceChannels()部分返回以下错误:

输入字符串格式不正确。不能存储在ServiceChannel列中。期望类型为Int32

如何使GridView显示外键的字符串值?

如何在GridView中显示外键值

解决方案是使ServiceChannel对象具有基于数据表的ID属性,并在GetDepartments方法中匹配该对象的ID:

Department tmpDepartment = new Department(departmentRow);
            tmpDepartment.ServiceChannel = channels.Where(c => c.Id == 
                   departmentRow.ServiceChannelID).FirstOrDefault();

则可以通过

在视图上访问该属性
e.Row.Cells[e.Row.Cells.Count - 1].Text = ((Department) 
                               e.Row.DataItem).ServiceChannel.Description;