gpt4 book ai didi

asp.net - 在 Gridview 中显示同名的两列

转载 作者:行者123 更新时间:2023-12-02 10:47:41 24 4
gpt4 key购买 nike

我有一个使用 JOIN 的 gridview。来自不同表的两列具有相同的名称:

<asp:GridView ID="viewThemeTypeAssociationsGridView" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
DataKeyNames="PK_User" CellPadding="3" CellSpacing="2" DataSourceID="SqlDataSource6"
OnRowDataBound="viewThemeTypeAssociationsGridView_OnRowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Name" HeaderText="Organization Team" SortExpression="Name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource6" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="
SELECT [User].[PK_User]
,[User].[Name]
,[OrganizationalTeam].[Name]
FROM [User]
LEFT JOIN [OrganizationalTeam] ON [OrganizationalTeam].[PK_OrganizationalTeam] = [User].[OrganizationalTeamId]
WHERE COALESCE(cast([OrganizationalTeam].[InternalExternal] as varchar(255)), 'null') LIKE @internalExternalParam
GROUP BY [User].[Name]
,[OrganizationalTeam].[Name]
,[User].[PK_User]
">
<SelectParameters>
<asp:QueryStringParameter Name="internalExternalParam" DefaultValue="%" Type="String" />
</SelectParameters>

我在显示它时遇到问题。我假设完成的方式如下:

<asp:BoundField DataField="[OrganizationalTeam].[Name]" HeaderText=" Team Name" SortExpression="[OrganizationalTeam].[Name]" />
<asp:BoundField DataField="[User].[Name]" HeaderText="Team" SortExpression="[User].[Name]" />

这会导致以下错误:

User 既不是表 DefaultView 的 DataColumn,也不是 DataRelation。

如何呈现不同表中具有相同名称的两列。

最佳答案

问题是该列实际上是两者的Name,我的建议是对它们使用别名来区分每个表中的数据:

SELECT [User].[PK_User]
,[User].[Name] as UserName
,[OrganizationalTeam].[Name] as OrganizationalTeamName
FROM [User]
LEFT JOIN [OrganizationalTeam] ON [OrganizationalTeam].[PK_OrganizationalTeam] = [User].[OrganizationalTeamId]
WHERE COALESCE(cast([OrganizationalTeam].[InternalExternal] as varchar(255)), 'null') LIKE @internalExternalParam
GROUP BY [User].[Name]
,[OrganizationalTeam].[Name]

关于asp.net - 在 Gridview 中显示同名的两列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18989618/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com