GridView 控件中的复杂数据绑定

Complex Data Binding in GridView control

我有几个 table 使用主键关联在一起。当我想使用 GridView 和数据绑定显示 table 时。 GridView 显示相关 table 的主键,这对我没有用!我想要显示相关 table 的另一列(但我仍然希望能够使用相关 table 的行的 ID 进行编辑)。

<asp:GridView ID="GridView1" runat="server" 
     OnSelectedIndexChanged="GridView1_SelectedIndexChanged" 
     AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
     DataKeyNames="Id" DataSourceID="FaiulerDataSource">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:BoundField DataField="Id" HeaderText="Id" InsertVisible="False" ReadOnly="True" SortExpression="Id" />
        <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
        <asp:BoundField DataField="FaiulerDiscription" HeaderText="FaiulerDiscription" SortExpression="FaiulerDiscription" />
        <asp:BoundField DataField="ShiftMaster" HeaderText="ShiftMaster" SortExpression="ShiftMaster" />
        <asp:CheckBoxField DataField="Approval" HeaderText="Approval" SortExpression="Approval" />
        <asp:BoundField DataField="RepairManId" HeaderText="RepairManId" SortExpression="RepairManId" />
        <asp:BoundField DataField="OperatorId" HeaderText="OperatorId" SortExpression="OperatorId" />
        <asp:BoundField DataField="Part_Id" HeaderText="Part_Id" SortExpression="Part_Id" />
        <asp:BoundField DataField="Shift_Id" HeaderText="Shift_Id" SortExpression="Shift_Id" />
        <asp:BoundField DataField="FaultType_Id" HeaderText="FaultType_Id" SortExpression="FaultType_Id" />
        <asp:BoundField DataField="Machine_Id" HeaderText="Machine_Id" SortExpression="Machine_Id" />
        <asp:BoundField DataField="RepairType_Id" HeaderText="RepairType_Id" SortExpression="RepairType_Id" />
    </Columns>
</asp:GridView>

如您所见,Faiuler table 包含许多列。我还有其他 table 称为 RepairManMachinesShift 等,因此此代码显示 repairman_id 但我希望 RepairMan_Name 是显示给用户,但我仍然需要 RepairMan_id 来编辑和删除内容。

更新:

这是 DataSource 的代码,可以告诉您如何更改它吗?我不太了解我使用向导创建数据源而不是编写代码。

 <asp:SqlDataSource ID="FaiulerDataSource" runat="server" 
      ConnectionString="<%$ ConnectionStrings:WoodFactoryConnectionString %>" 
      SelectCommand="SELECT [Id], [Date], [FaiulerDiscription], [ShiftMaster], [Approval], [RepairManId], [OperatorId], [Part_Id], [Shift_Id], [FaultType_Id], [Machine_Id], [RepairType_Id] FROM [Failures] WHERE ([RepairManId] IS NULL)">
</asp:SqlDataSource>

假设您已经将名称包含在数据源中,您可以使用以下方法绑定到名称

<asp:BoundField DataField="RepairMan_Name" HeaderText="repair name"  />

并且您的 sqldatasource 应该如下所示

SELECT [Id], [Date], [FaiulerDiscription], [ShiftMaster], [Approval], [RepairManId], [OperatorId], [Part_Id], [Shift_Id], [FaultType_Id], [Machine_Id], [RepairType_Id], [RepairMan.RepairMan_Name] AS RepairMan_Name FROM [Failures] inner join RepairMan on RepairMan.repairmanid = failures.repairmanid WHERE ([RepairManId] IS NULL)

您应该 select select 命令中的那个字段,例如:

<asp:SqlDataSource ID="FaiulerDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:WoodFactoryConnectionString %>" SelectCommand="SELECT [Id], [Date], [FaiulerDiscription], [ShiftMaster], [Approval], [RepairManId], [OperatorId], [Part_Id], [Shift_Id], [FaultType_Id], [Machine_Id], [RepairType_Id],RepairMan.RepairMan_Name FROM [Failures],[RepairMan] WHERE (Failures.RepairManId IS NULL)"></asp:SqlDataSource>

如果它们之间存在关系,我想 RepairManFailures 表之间存在 one-to-many 关系,这意味着一个 RepairMan 可以有多个 [=15] =] 所以你应该加入这两个并更改 select 命令,如:

SELECT Failures.Id, Failures.Date, Failures.FaiulerDiscription, Failures.ShiftMaster, Failures.Approval,
 Failures.RepairManId,Failures.OperatorId, Failures.Part_Id, Failures.Shift_Id, Failures.FaultType_Id, Failures.Machine_Id, Failures.RepairType_Id,RepairMan.RepairMan_Name,RepairMan.FauilarId
 FROM [Failures] inner join RepairMan on RepairMan.RepairMan_Id = Failures.RepairManId WHERE (Failures.RepairManId IS NULL)

然后你可以像这样绑定名称:

 <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />