网格视图外键表示

时间:2014-01-30 11:50:20

标签: asp.net sql database vb.net gridview

我一直试图在保留原始值的同时从另一个表中插入一个值,以便向用户说明他们正在选择的内容(对不起它' s非常难以解释。)

这是我当前的sql源和Select Command: 选择命令:#要明确以下代码,问题显而易见!

SelectCommand="SELECT c.ClassID, c.TeacherID, c.ClassYear, c.SubjectID, t.TeacherForename FROM (Class c INNER JOIN Teacher t ON c.TeacherID = t.TeacherID), s.SubjectName FROM (Class c INNER JOIN Subject s ON c.SubjectID = s.SubjectID)" 

SQL数据源&#39>:

 <asp:TemplateField HeaderText="SubjectName">
            <ItemTemplate>
            <%# Eval("SubjectName")%>
            </ItemTemplate>
             <EditItemTemplate>
                <asp:DropDownList ID="DropDownList2" runat="server" 
                    DataSourceID="SqlDataSource2" DataTextField="SubjectName" 
                    DataValueField="SubjectID" SelectedValue='<%# Bind("SubjectID") %>'>
                </asp:DropDownList>
                <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                    ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                    SelectCommand="SELECT Subject.SubjectName, Class.SubjectID FROM (Class INNER JOIN Subject ON Class.SubjectID = Subject.SubjectID)">
                </asp:SqlDataSource>
             </EditItemTemplate>
          </asp:TemplateField>

          <asp:TemplateField HeaderText="TeacherName">
            <ItemTemplate>
            <%# Eval("TeacherForename") %>
            </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server" 
                        DataSourceID="SqlDataSource3" DataTextField="TeacherForename" 
                        DataValueField="TeacherID" SelectedValue='<%# Bind("TeacherID") %>'>
                    </asp:DropDownList>
                    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                        ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
                        SelectCommand="SELECT Teacher.TeacherForename, Class.TeacherID FROM (Class INNER JOIN Teacher ON Class.TeacherID = Teacher.TeacherID)">
                    </asp:SqlDataSource>
                </EditItemTemplate>
          </asp:TemplateField>

最终结果我希望通过一个包含SubjectName和TeacherForename值的下拉选项,在gridview中表示SubjectID和TeacherID的当前值(当用户希望编辑它们时)(我希望这会让它更清晰) )。

但它在我的select命令中说我的FROM语句存在问题,我尝试使用TeacherID到TeacherForename的这个select命令并且它工作正常,但是一旦我将SubjectID添加到SubjectName它创造了一个错误,我无法找到它。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

尝试此查询..

SELECT 
c.ClassID
, c.TeacherID
, c.ClassYear
, c.SubjectID
, t.TeacherForename
, s.SubjectName

 FROM Class c 
 INNER JOIN Teacher t ON c.TeacherID = t.TeacherID
 INNER JOIN Subject s ON c.SubjectID = s.SubjectID