【问题标题】:How to display value of foreign key in GridView?如何在 GridView 中显示外键的值?
【发布时间】:2014-03-18 08:32:02
【问题描述】:

在 Web 窗体 ASP.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、描述和连接到部门的 ServiceChannel 选择部门。

我在类型化数据集中使用的这个存储过程带有一个名为DepartmentTableAdapter,它的方法是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获取该值,将结果存储在数据表中并将结果循环到其中并添加数据行并将结果一一存储在其中,然后将数据表与网格视图

标签: c# asp.net tsql gridview foreign-keys


【解决方案1】:

解决方案是根据数据表制作具有ID属性的ServiceChannel对象,并在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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2016-03-17
    • 2021-12-02
    • 2019-01-31
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多