我正在尝试使用下拉列表过滤gridview。我的gridview和下拉列表正由Access数据库填充。我想要的是当从下拉列表中选择供应商时,它会将产品的网格视图过滤到具有该供应商的人。
我可以填充下拉列表和gridview,但是当我选择供应商时,没有任何事情发生。
我尝试添加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>
我错过了什么吗? 感谢。
答案 0 :(得分: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>