1

我正在尝试使用下拉列表过滤网格视图。我的 gridview 和下拉列表由 Access 数据库填充。我想要的是当从下拉列表中选择供应商时,它会将产品的网格视图过滤到具有该供应商ID的产品。

我能够填充下拉列表和网格视图,但是当我选择供应商时没有任何反应。

我尝试添加 FilterExpression="SupplierID Like '{0}%'",但随后页面错误无法在 System.Int32 和 System.String 上执行 'Like' 操作

这是我的代码。

<form id="form" runat="server" style="margin:0 auto;">
<asp:DropDownList ID="DropDownList1" runat="server" 
    DataSourceID="AccessDataSource2" DataTextField="Supplier" DataValueField="ID" AutoPostBack="true" AppendDataBoundItems="true">
    <asp:ListItem Text="All" Value="%" />
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource2" runat="server" 
    DataFile="~/App_Data/Hovden Oil Pricing.accdb" 
    SelectCommand="SELECT [Supplier], [ID] FROM [Suppliers] ORDER BY [Supplier]">
</asp:AccessDataSource>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" BackColor="White"
    DataSourceID="AccessDataSource1">

    <Columns>
        <asp:BoundField DataField="SupplierID" HeaderText="SupplierID" 
            SortExpression="SupplierID" />
        <asp:BoundField DataField="Product Number" HeaderText="Product Number" 
            SortExpression="Product Number" />
        <asp:BoundField DataField="Product Name" HeaderText="Product Name" 
            SortExpression="Product Name" />
        <asp:BoundField DataField="Product Type" HeaderText="Product Type" 
            SortExpression="Product Type" />
        <asp:CheckBoxField DataField="Bulked" HeaderText="Bulked" 
            SortExpression="Bulked" />
        <asp:BoundField DataField="UOM" HeaderText="UOM" SortExpression="UOM" />
        <asp:BoundField DataField="Multiple" HeaderText="Multiple" 
            SortExpression="Multiple" />
        <asp:BoundField DataField="$/GAL" HeaderText="$/GAL" SortExpression="$/GAL" />
        <asp:BoundField DataField="$/UNIT" HeaderText="$/UNIT" ReadOnly="True" 
            SortExpression="$/UNIT" />
        <asp:BoundField DataField="TypeID" HeaderText="TypeID" 
            SortExpression="TypeID" />
    </Columns>

</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/Hovden Oil Pricing.accdb" SelectCommand="SELECT [Supplier Products].SupplierID, Products.[Product Number], Products.[Product Name], [Product Types].[Product Type], Products.Bulked, Products.UOM, Products.Multiple, [Bulk Prices].Volume AS [$/GAL], [$/GAL]*[Multiple] AS [$/UNIT], [Product Types].TypeID
    FROM [Product Types] INNER JOIN ((Products INNER JOIN [Supplier Products] ON Products.[Product Number] = [Supplier Products].[Product Number]) INNER JOIN [Bulk Prices] ON (Products.[Product Number] = [Bulk Prices].[Product Number]) AND (Products.[Product Number] = [Bulk Prices].[Product Number])) ON [Product Types].TypeID = Products.[Product Type]
    WHERE (Products.Bulked)=Yes
    ORDER BY Products.[Product Name];" FilterExpression="SupplierID Like '{0}%'">
    <FilterParameters>
        <asp:ControlParameter Name="SupplierID" ControlID="DropDownList1" PropertyName="SelectedValue" />
    </FilterParameters>
</asp:AccessDataSource>
</form>

我错过了什么吗?谢谢。

4

1 回答 1

2

这真的很简单,您正在尝试将 int(SupplierID) 与通配符字符串值进行比较,这就是您收到上述错误的原因。

只需将 SupplierID 转换为字符串即可:

<asp:AccessDataSource ID="AccessDataSource1" runat="server" 
    DataFile="~/App_Data/Hovden Oil Pricing.accdb" SelectCommand="SELECT [Supplier Products].SupplierID, Products.[Product Number], Products.[Product Name], [Product Types].[Product Type], Products.Bulked, Products.UOM, Products.Multiple, [Bulk Prices].Volume AS [$/GAL], [$/GAL]*[Multiple] AS [$/UNIT], [Product Types].TypeID
    FROM [Product Types] INNER JOIN ((Products INNER JOIN [Supplier Products] ON Products.[Product Number] = [Supplier Products].[Product Number]) INNER JOIN [Bulk Prices] ON (Products.[Product Number] = [Bulk Prices].[Product Number]) AND (Products.[Product Number] = [Bulk Prices].[Product Number])) ON [Product Types].TypeID = Products.[Product Type]
    WHERE (Products.Bulked)=Yes
    ORDER BY Products.[Product Name];" FilterExpression="Convert(SupplierID , 'System.String') LIKE '{0}%'">
    <FilterParameters>
        <asp:ControlParameter Name="SupplierID" ControlID="DropDownList1" PropertyName="SelectedValue" />
    </FilterParameters>
于 2013-01-25T20:32:17.000 回答