2014-07-15 485 views
0

我無法使用下面的數據源將GridView綁定到DataRowView,但是如果我使用通配符,它​​可以工作。Gridview數據綁定錯誤

失敗: 錯誤:數據綁定: 'System.Data.DataRowView' 不包含與名稱的屬性 'EnrollmentsTbl.AutoNum'

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataKeyNames="EnrollmentsTbl.AutoNum" 
DataSourceID="AccessDataSource1" ForeColor="Black" GridLines="Vertical" PageSize="100"> 
    <PagerSettings Mode="NumericFirstLast" PageButtonCount="75" /> 
    <FooterStyle BackColor="#CCCCCC" /> 
    <Columns> 
     <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
     <asp:BoundField DataField="EnrollmentsTbl.AutoNum" HeaderText="AutoNum" InsertVisible="False" ReadOnly="True" SortExpression="EnrollmentsTbl.AutoNum" /> 
     <asp:BoundField DataField="SubmitTime" HeaderText="SubmitTime" SortExpression="SubmitTime" /> 
     <asp:BoundField DataField="ClassName" HeaderText="ClassName" SortExpression="ClassName" /> 
     <asp:BoundField DataField="ClassDate" HeaderText="ClassDate" SortExpression="ClassDate" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False"/> 
     <asp:BoundField DataField="DateCompleted" HeaderText="DateCompleted" SortExpression="DateCompleted" DataFormatString="{0:MM/dd/yyyy}" HtmlEncode="False"/> 
     <asp:CheckBoxField DataField="Completed" HeaderText="Completed" SortExpression="Completed" /> 
     <asp:BoundField DataField="EnrollmentsTbl.UID" HeaderText="UID" ReadOnly="True" SortExpression="EnrollmentsTbl.UID" /> 
    </Columns> 
    <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> 
    <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" /> 
    <AlternatingRowStyle BackColor="#6699CC" /> 
</asp:GridView> 

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
DataFile="<%$ ConnectionStrings:AccessSubSiteString %>" 
    DeleteCommand="DELETE FROM [EnrollmentsTbl] WHERE [EnrollmentsTbl.AutoNum] = ?" 
    SelectCommand="SELECT EnrollmentsTbl.AutoNum, EnrollmentsTbl.SubmitTime, EnrollmentsTbl.ClassName, 
    EnrollmentsTbl.ClassDate, EnrollmentsTbl.DateCompleted, EnrollmentsTbl.Completed, EnrollmentsTbl.UID 
    FROM [EnrollmentsTbl] INNER JOIN [UsersDataTbl] ON EnrollmentsTbl.UID = UsersDataTbl.UID 
    WHERE ([EnrollmentsTbl.AutoNum] > ?)" 
    UpdateCommand="UPDATE [EnrollmentsTbl] SET [UserName] = ?, [SubmitTime] = ?, [ClassName] = ?, [ClassDate] = ?, 
    [ClassTime] = ?, [Enrolled] = ?, [WaitListed] = ?, [Instructor] = ?, [DateCompleted] = ?, [Completed] = ?, [Walkin] = ? 
    WHERE [EnrollmentsTbl.AutoNum] = ?"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="UserNameTB" DefaultValue="" Name="?" PropertyName="Text" /> 
    </SelectParameters> 
    <DeleteParameters> 
     <asp:Parameter Name="EnrollmentsTbl.AutoNum" Type="Int32" /> 
    </DeleteParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="UserName" Type="String" /> 
     <asp:Parameter Name="SubmitTime" Type="DateTime" /> 
     <asp:Parameter Name="ClassName" Type="String" /> 
     <asp:Parameter Name="ClassDate" Type="DateTime" /> 
     <asp:Parameter Name="ClassTime" Type="String" /> 
     <asp:Parameter Name="Enrolled" Type="Boolean" /> 
     <asp:Parameter Name="WaitListed" Type="Boolean" /> 
     <asp:Parameter Name="Instructor" Type="String" /> 
     <asp:Parameter Name="DateCompleted" Type="DateTime" /> 
     <asp:Parameter Name="Completed" Type="Boolean" /> 
     <asp:Parameter Name="Walkin" Type="Boolean" /> 
     <asp:Parameter Name="EnrollmentsTbl.AutoNum" Type="Int32" /> 
    </UpdateParameters> 
</asp:AccessDataSource> 

但是: 改變SELECTCOMMAND="SELECT * FROM...允許它的工作?

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
DataFile="<%$ ConnectionStrings:AccessSubSiteString %>" 
    DeleteCommand="DELETE FROM [EnrollmentsTbl] WHERE [EnrollmentsTbl.AutoNum] = ?" 
    SelectCommand="SELECT * 
    FROM [EnrollmentsTbl] INNER JOIN [UsersDataTbl] ON EnrollmentsTbl.UID = UsersDataTbl.UID 
    WHERE ([EnrollmentsTbl.AutoNum] > ?)" 
    UpdateCommand="UPDATE [EnrollmentsTbl] SET [UserName] = ?, [SubmitTime] = ?, [ClassName] = ?, [ClassDate] = ?, 
    [ClassTime] = ?, [Enrolled] = ?, [WaitListed] = ?, [Instructor] = ?, [DateCompleted] = ?, [Completed] = ?, [Walkin] = ? 
    WHERE [EnrollmentsTbl.AutoNum] = ?"> 
    <SelectParameters> 
     <asp:ControlParameter ControlID="UserNameTB" DefaultValue="" Name="?" PropertyName="Text" /> 
    </SelectParameters> 
    <DeleteParameters> 
     <asp:Parameter Name="EnrollmentsTbl.AutoNum" Type="Int32" /> 
    </DeleteParameters> 
    <UpdateParameters> 
     <asp:Parameter Name="UserName" Type="String" /> 
     <asp:Parameter Name="SubmitTime" Type="DateTime" /> 
     <asp:Parameter Name="ClassName" Type="String" /> 
     <asp:Parameter Name="ClassDate" Type="DateTime" /> 
     <asp:Parameter Name="ClassTime" Type="String" /> 
     <asp:Parameter Name="Enrolled" Type="Boolean" /> 
     <asp:Parameter Name="WaitListed" Type="Boolean" /> 
     <asp:Parameter Name="Instructor" Type="String" /> 
     <asp:Parameter Name="DateCompleted" Type="DateTime" /> 
     <asp:Parameter Name="Completed" Type="Boolean" /> 
     <asp:Parameter Name="Walkin" Type="Boolean" /> 
     <asp:Parameter Name="EnrollmentsTbl.AutoNum" Type="Int32" /> 
    </UpdateParameters> 
</asp:AccessDataSource> 

我需要gridview中bith tabes的字段,所以通配符不起作用。

回答

0

沒有幫助提供,但它似乎我發現了一個分辨率/工作一個輪:

問題的

部分是,當通配符(*)在SELECT語句從返回(SelectCommand)領域使用INNER JOIN是混合的,有些有table.column和其他只是列。例如:如果兩個表都包含AutoNum,則返回table1.AutoNum和table2.AutNum - 但如果只有一個表具有字段,則返回值僅爲Field - 這是在gridview中設置列名的問題,問題。