多个 DataKeyNames 不过滤 sqldatasource
Multiple DataKeyNames not filtering sqldatasource
- 我在一个页面上有两个网格视图都绑定到一个 sqldatasource。第一个 gridview (GVMonthReport) 显示来自 table
的数据
- 我的目标是 select GVMonthReport 中的一行来过滤另一个 table(GridView2) sqldatasource 中的三个项目(位置,Trans_Budget_Code,交易日期)。
- 问题如下:我可以相应地使用其中一个过滤项目(Location 或 Trans_Budget_Code)和 GridView2 过滤器,两者都有效。但是,当我同时尝试将 Location 和 Trans_Budget_Code 作为过滤器时,GridView2 上没有显示任何内容。我可以手动将 Trans_Budget_Code 和 Location 的值放入 GridView2s sqldatasource 中,结果是我所期望的。为什么这行得通,但当我在 GVMonthReport 中 select 一行时却行不通。
- 我认为它与 GVMonthReports DataKeyNames ("Trans_Budget_Code,Location") 有关。但到目前为止我没有运气。 Trans_Budget_Code 和 Location 不是主键是那个问题?
<asp:SqlDataSource ID="MonthUsed0" runat="server" , SelectCommand="SELECT DISTINCT dbo.[ID Inventory$].[Item Description], dbo.[ID Transactions This Week$].Location, dbo.[ID Transactions This Week$].[Trns Date], dbo.[ID Transactions This Week$].Budget, dbo.[ID Inventory$].[Item Number] FROM dbo.[ID Transactions This Week$] INNER JOIN dbo.[ID Inventory$] ON dbo.[ID Transactions This Week$].[Item Number] = dbo.[ID Inventory$].[Item Number] WHERE (dbo.[ID Transactions This Week$].Location = @Location) AND (dbo.[ID Transactions This Week$].Budget = @Trans_Budget_Code)"
UpdateCommand="UPDATE zBudgetDetails SET Budget_ID = @Budget_ID, Budget_Account_ID = @Budget_Account_ID, Amount = @Amount, Memo = @Memo WHERE (Budget_Detail_ID = @Budget_Detail_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="GVMonthReport" Name="Location" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GVMonthReport" Name="Trans_Budget_Code" PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Budget_ID" Type="Int32" />
<asp:Parameter Name="Budget_Account_ID" Type="Int32" />
<asp:Parameter Name="Amount" Type="Decimal" />
<asp:Parameter Name="Memo" Type="String" />
<asp:Parameter Name="Budget_Detail_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="MonthUsed0" style="margin-top: 1px">
<Columns>
<asp:BoundField DataField="Item Description" HeaderText="Item Description" SortExpression="Item Description" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="Trns Date" HeaderText="Trns Date" SortExpression="Trns Date" />
<asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget" />
<asp:BoundField DataField="Item Number" HeaderText="Item Number" SortExpression="Item Number" />
</Columns>
</asp:GridView>
<br />
<br />
<br />
<asp:GridView ID="GVMonthReport" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames="Trans_Budget_Code,Location" AutoGenerateColumns="False" CellPadding="4" DataSourceID="MonthReport" ForeColor="#333333" GridLines="None" style="margin-right: 0px; margin-top: 0px;">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="No#" HeaderText="No#" ReadOnly="True" SortExpression="No#" />
<asp:BoundField DataField="Transaction Type" HeaderText="Transaction Type" ReadOnly="True" SortExpression="Transaction Type" />
<asp:BoundField DataField="Trans_Budget_Code" HeaderText="Trans_Budget_Code" ReadOnly="True" SortExpression="Trans_Budget_Code" />
<asp:BoundField DataField="Location" HeaderText="Location" ReadOnly="True" SortExpression="Location" />
<asp:BoundField DataField="Supplier/Employee Name" HeaderText="Supplier/Employee Name" ReadOnly="True" SortExpression="Supplier/Employee Name" />
<asp:BoundField DataField="Project Code" HeaderText="Project Code" ReadOnly="True" SortExpression="Project Code" />
<asp:BoundField DataField="WOID" HeaderText="WOID" ReadOnly="True" SortExpression="WOID" />
<asp:BoundField DataField="Transaction Date" HeaderText="Transaction Date" ReadOnly="True" SortExpression="Transaction Date" />
<asp:BoundField DataField="Craft" HeaderText="Craft" ReadOnly="True" SortExpression="Craft" />
<asp:BoundField DataField="Transaction Description" HeaderText="Transaction Description" ReadOnly="True" SortExpression="Transaction Description" />
<asp:BoundField DataField="Purpose Description" HeaderText="Purpose Description" ReadOnly="True" SortExpression="Purpose Description" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" ReadOnly="True" SortExpression="Quantity" />
<asp:BoundField DataField="Unit Cost" HeaderText="Unit Cost" ReadOnly="True" SortExpression="Unit Cost" />
<asp:BoundField DataField="Total Cost" HeaderText="Total Cost" ReadOnly="True" SortExpression="Total Cost" />
<asp:BoundField DataField="Purchased by" HeaderText="Purchased by" ReadOnly="True" SortExpression="Purchased by" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<br />
<asp:SqlDataSource ID="MonthReport" runat="server" SelectCommand="SELECT No#, [Transaction Type], Trans_Budget_Code, Location, [Supplier/Employee Name], [Project Code], WOID, [Transaction Date], Craft, [Transaction Description], [Purpose Description], Quantity, [Unit Cost], [Total Cost], [Purchased by] FROM dbo.[MD Transactions] WHERE ([WO Budget Code] = @Acount) AND (YEAR([Transaction Date]) = @yeardate) AND (MONTH([Transaction Date]) = @monthdate) AND (Trans_Budget_Code = 'Maintenance') UNION ALL SELECT No#, [Transaction Type], Trans_Budget_Code, Location, [Supplier/Employee Name], [Project Code], WOID, [Transaction Date], Craft, [Transaction Description], [Purpose Description], Quantity, [Unit Cost], [Total Cost], [Purchased by] FROM dbo.[MD Transactions This Week$] WHERE ([WO Budget Code] = @Acount) AND (YEAR([Transaction Date]) = @yeardate) AND (MONTH([Transaction Date]) = @monthdate) AND (Trans_Budget_Code = 'Maintenance')"
UpdateCommand="UPDATE zBudgetDetails SET Budget_ID = @Budget_ID, Budget_Account_ID = @Budget_Account_ID, Amount = @Amount, Memo = @Memo WHERE (Budget_Detail_ID = @Budget_Detail_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="DDLMonthAcount" Name="Acount" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DDLYear" Name="yeardate" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DDLMonth" Name="monthdate" PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Budget_ID" Type="Int32" />
<asp:Parameter Name="Budget_Account_ID" Type="Int32" />
<asp:Parameter Name="Amount" Type="Decimal" />
<asp:Parameter Name="Memo" Type="String" />
<asp:Parameter Name="Budget_Detail_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
为 <asp:SqlDataSource ID="MonthUsed0" .../>
定义的设置似乎可能存在问题
<asp:ControlParameter .../>
的PropertyName
属性应该设置如下
<SelectParameters>
<asp:ControlParameter ControlID="GVMonthReport" Name="Location"
PropertyName="SelectedDataKey.Values[1]" />
<asp:ControlParameter ControlID="GVMonthReport"
Name="Trans_Budget_Code" PropertyName="SelectedDataKey.Values[0]" />
</SelectParameters>
这里的[0]和[1]表示GridViewDataKeyNames
属性中定义的Keys的位置
设置 PropertyName="SelectedValue"
将仅使用 DataKeyNames
属性 中定义的第一个列的值。
您可以将 PropertyName
参数显式设置为列名,如:
PropertyName="SelectedDataKey.Values["Location"]"
- 我在一个页面上有两个网格视图都绑定到一个 sqldatasource。第一个 gridview (GVMonthReport) 显示来自 table 的数据
- 我的目标是 select GVMonthReport 中的一行来过滤另一个 table(GridView2) sqldatasource 中的三个项目(位置,Trans_Budget_Code,交易日期)。
- 问题如下:我可以相应地使用其中一个过滤项目(Location 或 Trans_Budget_Code)和 GridView2 过滤器,两者都有效。但是,当我同时尝试将 Location 和 Trans_Budget_Code 作为过滤器时,GridView2 上没有显示任何内容。我可以手动将 Trans_Budget_Code 和 Location 的值放入 GridView2s sqldatasource 中,结果是我所期望的。为什么这行得通,但当我在 GVMonthReport 中 select 一行时却行不通。
- 我认为它与 GVMonthReports DataKeyNames ("Trans_Budget_Code,Location") 有关。但到目前为止我没有运气。 Trans_Budget_Code 和 Location 不是主键是那个问题?
<asp:SqlDataSource ID="MonthUsed0" runat="server" , SelectCommand="SELECT DISTINCT dbo.[ID Inventory$].[Item Description], dbo.[ID Transactions This Week$].Location, dbo.[ID Transactions This Week$].[Trns Date], dbo.[ID Transactions This Week$].Budget, dbo.[ID Inventory$].[Item Number] FROM dbo.[ID Transactions This Week$] INNER JOIN dbo.[ID Inventory$] ON dbo.[ID Transactions This Week$].[Item Number] = dbo.[ID Inventory$].[Item Number] WHERE (dbo.[ID Transactions This Week$].Location = @Location) AND (dbo.[ID Transactions This Week$].Budget = @Trans_Budget_Code)"
UpdateCommand="UPDATE zBudgetDetails SET Budget_ID = @Budget_ID, Budget_Account_ID = @Budget_Account_ID, Amount = @Amount, Memo = @Memo WHERE (Budget_Detail_ID = @Budget_Detail_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="GVMonthReport" Name="Location" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="GVMonthReport" Name="Trans_Budget_Code" PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Budget_ID" Type="Int32" />
<asp:Parameter Name="Budget_Account_ID" Type="Int32" />
<asp:Parameter Name="Amount" Type="Decimal" />
<asp:Parameter Name="Memo" Type="String" />
<asp:Parameter Name="Budget_Detail_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="MonthUsed0" style="margin-top: 1px">
<Columns>
<asp:BoundField DataField="Item Description" HeaderText="Item Description" SortExpression="Item Description" />
<asp:BoundField DataField="Location" HeaderText="Location" SortExpression="Location" />
<asp:BoundField DataField="Trns Date" HeaderText="Trns Date" SortExpression="Trns Date" />
<asp:BoundField DataField="Budget" HeaderText="Budget" SortExpression="Budget" />
<asp:BoundField DataField="Item Number" HeaderText="Item Number" SortExpression="Item Number" />
</Columns>
</asp:GridView>
<br />
<br />
<br />
<asp:GridView ID="GVMonthReport" runat="server" AllowPaging="True" AllowSorting="True" DataKeyNames="Trans_Budget_Code,Location" AutoGenerateColumns="False" CellPadding="4" DataSourceID="MonthReport" ForeColor="#333333" GridLines="None" style="margin-right: 0px; margin-top: 0px;">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="No#" HeaderText="No#" ReadOnly="True" SortExpression="No#" />
<asp:BoundField DataField="Transaction Type" HeaderText="Transaction Type" ReadOnly="True" SortExpression="Transaction Type" />
<asp:BoundField DataField="Trans_Budget_Code" HeaderText="Trans_Budget_Code" ReadOnly="True" SortExpression="Trans_Budget_Code" />
<asp:BoundField DataField="Location" HeaderText="Location" ReadOnly="True" SortExpression="Location" />
<asp:BoundField DataField="Supplier/Employee Name" HeaderText="Supplier/Employee Name" ReadOnly="True" SortExpression="Supplier/Employee Name" />
<asp:BoundField DataField="Project Code" HeaderText="Project Code" ReadOnly="True" SortExpression="Project Code" />
<asp:BoundField DataField="WOID" HeaderText="WOID" ReadOnly="True" SortExpression="WOID" />
<asp:BoundField DataField="Transaction Date" HeaderText="Transaction Date" ReadOnly="True" SortExpression="Transaction Date" />
<asp:BoundField DataField="Craft" HeaderText="Craft" ReadOnly="True" SortExpression="Craft" />
<asp:BoundField DataField="Transaction Description" HeaderText="Transaction Description" ReadOnly="True" SortExpression="Transaction Description" />
<asp:BoundField DataField="Purpose Description" HeaderText="Purpose Description" ReadOnly="True" SortExpression="Purpose Description" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" ReadOnly="True" SortExpression="Quantity" />
<asp:BoundField DataField="Unit Cost" HeaderText="Unit Cost" ReadOnly="True" SortExpression="Unit Cost" />
<asp:BoundField DataField="Total Cost" HeaderText="Total Cost" ReadOnly="True" SortExpression="Total Cost" />
<asp:BoundField DataField="Purchased by" HeaderText="Purchased by" ReadOnly="True" SortExpression="Purchased by" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<br />
<asp:SqlDataSource ID="MonthReport" runat="server" SelectCommand="SELECT No#, [Transaction Type], Trans_Budget_Code, Location, [Supplier/Employee Name], [Project Code], WOID, [Transaction Date], Craft, [Transaction Description], [Purpose Description], Quantity, [Unit Cost], [Total Cost], [Purchased by] FROM dbo.[MD Transactions] WHERE ([WO Budget Code] = @Acount) AND (YEAR([Transaction Date]) = @yeardate) AND (MONTH([Transaction Date]) = @monthdate) AND (Trans_Budget_Code = 'Maintenance') UNION ALL SELECT No#, [Transaction Type], Trans_Budget_Code, Location, [Supplier/Employee Name], [Project Code], WOID, [Transaction Date], Craft, [Transaction Description], [Purpose Description], Quantity, [Unit Cost], [Total Cost], [Purchased by] FROM dbo.[MD Transactions This Week$] WHERE ([WO Budget Code] = @Acount) AND (YEAR([Transaction Date]) = @yeardate) AND (MONTH([Transaction Date]) = @monthdate) AND (Trans_Budget_Code = 'Maintenance')"
UpdateCommand="UPDATE zBudgetDetails SET Budget_ID = @Budget_ID, Budget_Account_ID = @Budget_Account_ID, Amount = @Amount, Memo = @Memo WHERE (Budget_Detail_ID = @Budget_Detail_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="DDLMonthAcount" Name="Acount" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DDLYear" Name="yeardate" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="DDLMonth" Name="monthdate" PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Budget_ID" Type="Int32" />
<asp:Parameter Name="Budget_Account_ID" Type="Int32" />
<asp:Parameter Name="Amount" Type="Decimal" />
<asp:Parameter Name="Memo" Type="String" />
<asp:Parameter Name="Budget_Detail_ID" Type="Int32" />
</UpdateParameters>
</asp:SqlDataSource>
为 <asp:SqlDataSource ID="MonthUsed0" .../>
<asp:ControlParameter .../>
的PropertyName
属性应该设置如下
<SelectParameters>
<asp:ControlParameter ControlID="GVMonthReport" Name="Location"
PropertyName="SelectedDataKey.Values[1]" />
<asp:ControlParameter ControlID="GVMonthReport"
Name="Trans_Budget_Code" PropertyName="SelectedDataKey.Values[0]" />
</SelectParameters>
这里的[0]和[1]表示GridViewDataKeyNames
属性中定义的Keys的位置
设置 PropertyName="SelectedValue"
将仅使用 DataKeyNames
属性 中定义的第一个列的值。
您可以将 PropertyName
参数显式设置为列名,如:
PropertyName="SelectedDataKey.Values["Location"]"