使用SQL的GridView搜索/过滤器

时间:2019-04-21 08:12:49

标签: c# sql gridview webforms

我在c#Webform中获得了一个gridview,其中包含来自SQL Server数据库(通过sqlDataSource)的数据库。 SqlDataSource包含一个SQL字符串

SELECT * FROM [MaterialView] WHERE (([MaterialName] LIKE '%' + @MaterialName + '%') AND ([SupplierID] = @SupplierID))    

SQL字符串按SupplierID和MaterialName两次过滤数据。

首先,用户在上一页中确定SupplierID(@SupplierID参数),当加载gridview页面时,将执行([SupplierID] = @SupplierID)。

然后,我得到一个“搜索框” +“按钮”,使用户能够通过在“搜索框”(@MaterialName参数)中输入文本来再次过滤gridview,然后执行([[MaterialName] LIKE' %'+ @MaterialName +'%')。

我的问题是,当加载gridview页面时,由于“搜索框”最初是EMPTY(没有@MaterialName参数),因此gridview返回0记录()。

我要解决的方法是创建2个相似的Gridview,其中一个仅包含SupplierID过滤器,另一个则包含两个。

<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>

我希望使用一个gridview解决方案。当文本框为空(@MaterialName参数=“”)时,Gridview返回ALL @MaterialName而不是无@MaterialName。 谢谢。

2 个答案:

答案 0 :(得分:0)

您需要检查查询参数是否为null或长度是否为0或是否包含某个值,然后使用Like过滤

SELECT * FROM [MaterialView] 
WHERE 
((
 @MaterialName IS NULL 
 OR LEN(@MaterialName) = 0 
 OR ([MaterialName] LIKE '%' + @MaterialName + '%')
)
AND ([SupplierID] = @SupplierID));

答案 1 :(得分:0)

当我将文本框的默认文本设置为0(@MaterialName参数源)并将@ er-sho SQL修改为

时,它可以工作
usersId

所以我想问题是textbox.text的默认值(如果未设置)既不是NULL也不是LEN 0。 我知道这是一个草率的解决方案,但我对此感到满意,再次感谢@ er-sho