我有几个使用主键相关的表。当我想使用GridView和数据绑定显示表时。 GridView显示相关表的主键,对我没用!我想要显示相关表的另一列(但我仍然希望能够使用相关表的行的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
表包含许多列。我还有其他名为RepairMan
,Machines
,Shift
等的表格,因此此代码会显示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>
答案 0 :(得分:0)
您可以使用以下内容绑定到名称,假设您已在数据源中包含该名称
<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)
答案 1 :(得分:0)
您应该在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>
如果它们之间存在关联,我认为one-to-many
和RepairMan
表之间存在Failures
关系,这意味着一个RepairMan
可以有多个Failures
所以你应该加入这两个并改变选择命令,如:
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" />