如何使gridview可搜索?

时间:2011-05-17 12:11:20

标签: c# asp.net sql-server visual-studio

我有一个从多个表中抽取的gridview,我想让它可以在三个字段中搜索,每个表中有一个。我计划使用下拉列表选择要搜索的字段,当然还有一个文本框用于搜索输入,但是呢?这是我在C#ASP.NET中的gridview代码:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
    EmptyDataText="There are no data records to display." AllowPaging="True" 
    BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
    CellPadding="3" GridLines="Vertical" HorizontalAlign="Center" 
    DataKeyNames="SubId" AllowSorting="True">
    <AlternatingRowStyle BackColor="#DCDCDC" />

    <Columns>

        <asp:BoundField DataField="SubId" HeaderText="Submission ID" 
            SortExpression="SubId" >
        <ItemStyle HorizontalAlign="Right" VerticalAlign="Middle" />
        </asp:BoundField>
        <asp:BoundField DataField="CustName" HeaderText="Customer" 
            SortExpression="CustName" />
        <asp:BoundField DataField="CustCity" HeaderText="Customer City" 
            SortExpression="CustCity" />
        <asp:BoundField DataField="CustState" HeaderText="Customer State" 
            SortExpression="CustState" />
        <asp:BoundField DataField="BroName" HeaderText="Broker " 
            SortExpression="BroName" />
        <asp:BoundField DataField="BroState" HeaderText="Broker State" 
            SortExpression="BroState" />
        <asp:BoundField DataField="EntityType" HeaderText="EntityType" 
            SortExpression="EntityType" />
        <asp:BoundField DataField="Coverage" DataFormatString="{0:c}" 
            HeaderText="Coverage" SortExpression="Coverage" />
        <asp:BoundField DataField="Status" HeaderText="Status" 
            SortExpression="Status" />

        <asp:HyperLinkField DataNavigateUrlFields="SubId" 
            DataNavigateUrlFormatString="View.aspx?SubId={0}" Text="View" />
        <asp:HyperLinkField DataNavigateUrlFields="SubId" 
            DataNavigateUrlFormatString="ViewEdit.aspx?SubId={0}" Text="Edit" />

    </Columns>
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:MyConnectionString %>" 
    ProviderName="<%$ ConnectionStrings:ProductInstanceString.ProviderName %>" 


    SelectCommand="SELECT Customer.SubId, Customer.CustName, Customer.CustCity, Customer.CustState, Broker.BroName, Broker.BroState, Broker.EntityType, Submission.Coverage, Submission.Status FROM Submission INNER JOIN Broker ON Broker.SubId = Submission.SubmissionId INNER JOIN Customer ON Customer.SubId = Submission.SubmissionId">

</asp:SqlDataSource>

1 个答案:

答案 0 :(得分:1)

使用页面上的下拉列表和代码隐藏使用DataView对象来过滤网格行,请参阅以下语法:

DataView dvData = new DataView(dtData);
dvData.RowFilter = "[ColumnName]=[Condition, this will be your dropdown value]";
gv.DataSource = dvData;
gv.DataBind();