[英]Gridview search/filter using sql
I got a gridview in c# webform, which contains the database from SQL server database, through sqlDataSource.我在 c# webform 中得到了一个 gridview,它包含来自 SQL 服务器数据库的数据库,通过 sqlDataSource。 The SqlDataSource contains a SQL string SqlDataSource 包含一个 SQL 字符串
SELECT * FROM [MaterialView] WHERE (([MaterialName] LIKE '%' + @MaterialName + '%') AND ([SupplierID] = @SupplierID))
The SQL string filter the data twice, by SupplierID and MaterialName. SQL 字符串按供应商 ID 和材料名称过滤数据两次。
First the User determine SupplierID (@SupplierID parameter) on previous page, when gridview page is load, it execute ([SupplierID] = @SupplierID)).首先用户在上一页确定供应商ID(@SupplierID参数),当gridview页面加载时,它执行([SupplierID] = @SupplierID))。
And then, i got a "search box" + "button" which enable the the user to filter the gridview again, by entering text in "search box" (@MaterialName parameter), which execute ([MaterialName] LIKE '%' + @MaterialName + '%').然后,我得到了一个“搜索框”+“按钮”,它使用户能够通过在“搜索框”(@MaterialName 参数)中输入文本来再次过滤 gridview,执行 ([MaterialName] LIKE '%' + @材料名称 + '%')。
My way of fixing it is creating 2 similar Gridviews, one contains only supplierID filter,another contains both.我修复它的方法是创建 2 个类似的 Gridviews,一个只包含供应商 ID 过滤器,另一个包含两者。
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="40px" Width="289px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="搜尋" OnClick="Button1_Click" />
<!--初始表單-->
<asp:Panel ID="Panel1" runat="server">
<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" Height="498px" Width="712px" CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="auto-style1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ClassType" SortExpression="ClassType">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ClassType") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ClassType") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="MaterialName" SortExpression="MaterialName">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MaterialName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" commandname="choose" CommandArgument='<%# Eval("MaterialID") %>' Text='<%# Eval("MaterialName") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity" SortExpression="Quantity">
<EditItemTemplate>
<asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Quantity") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Quantity") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="MeasureUnit" HeaderStyle-width="10%" SortExpression="MeasureUnit">
<EditItemTemplate>
<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("MeasureUnit") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("MeasureUnit") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="10%" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:BoundField DataField="CreateDate" HeaderText="CreateDate" SortExpression="CreateDate" Visible="False" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" Visible="False" />
<asp:BoundField DataField="MaterialID" HeaderText="MaterialID" SortExpression="MaterialID" Visible="False" />
<asp:TemplateField HeaderText="SupplierName" SortExpression="SupplierName" Visible="False">
<EditItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("SupplierName") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SupplierName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</asp:Panel>
<!--過濾表單-->
<asp:Panel ID="Panel2" runat="server">
<asp:GridView ID="GridView2" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource3" Height="498px" Width="710px" CellPadding="4" ForeColor="#333333" GridLines="None" CssClass="auto-style1">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ClassType" HeaderText="ClassType" SortExpression="ClassType" />
<asp:BoundField DataField="MaterialName" HeaderText="MaterialName" SortExpression="MaterialName" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
<asp:BoundField DataField="MeasureUnit" HeaderText="MeasureUnit" SortExpression="MeasureUnit" />
<asp:BoundField DataField="CreateDate" HeaderText="CreateDate" SortExpression="CreateDate" Visible="False" />
<asp:BoundField DataField="MaterialID" HeaderText="MaterialID" SortExpression="MaterialID" Visible="False" />
<asp:BoundField DataField="SupplierID" HeaderText="SupplierID" SortExpression="SupplierID" Visible="False" />
<asp:BoundField DataField="SupplierName" HeaderText="SupplierName" SortExpression="SupplierName" Visible="False" />
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</asp:Panel>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:CompanyConnectionString2 %>" SelectCommand="SELECT * FROM [MaterialView] WHERE ([SupplierID] = @SupplierID)">
<SelectParameters>
<asp:SessionParameter Name="SupplierID" SessionField="Gsuply" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:CompanyConnectionString2 %>" SelectCommand="SELECT * FROM [MaterialView] WHERE (([MaterialName] LIKE '%' + @MaterialName + '%') AND ([SupplierID] = @SupplierID))">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="MaterialName" PropertyName="Text" Type="String" />
<asp:SessionParameter Name="SupplierID" SessionField="Gsuply" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</form>
I expect an One gridview solution.我期待 One gridview 解决方案。 When the textbox is empty (@MaterialName parameter = ""), the gridview return ALL @MaterialName instead of no @MaterialName.当文本框为空时(@MaterialName 参数 = ""),gridview 返回 ALL @MaterialName 而不是没有 @MaterialName。 Thank you.谢谢你。
You need to check whether your query parameter is null or its length is 0 or if its contains some value then filter with Like
您需要检查您的查询参数是否为空或其长度是否为 0 或者它是否包含某个值,然后使用Like
进行过滤
SELECT * FROM [MaterialView]
WHERE
((
@MaterialName IS NULL
OR LEN(@MaterialName) = 0
OR ([MaterialName] LIKE '%' + @MaterialName + '%')
)
AND ([SupplierID] = @SupplierID));
It works when I set textbox default text = 0 (@MaterialName parameter source), and modified @er-sho SQL into当我设置文本框默认文本 = 0(@MaterialName 参数源)并将@er-sho SQL 修改为
SELECT * FROM [MaterialView]
WHERE
((
@MaterialName = 0
OR ([MaterialName] LIKE '%' + @MaterialName + '%')
)
AND ([SupplierID] = @SupplierID));
So i guess the problem is textbox.text default value (if not set) is neither NULL or LEN 0. I know this is a sloppy solution but i'm satisfy with it, Thanks again @er-sho所以我想问题是 textbox.text 默认值(如果未设置)既不是 NULL 也不是 LEN 0。我知道这是一个草率的解决方案,但我很满意,再次感谢@er-sho
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.