在asp.net c#中搜索gridview

本文关键字:搜索 gridview asp net | 更新日期: 2023-09-27 18:03:55

我正试图寻找一种方法来搜索网格视图在asp.net使用c#编程语言。我不希望网格视图启用分页。我希望它能显示输入的结果。例如,如果我输入's',所有以s开头的记录将只可见。

我已经在代码中查找了一些有数据绑定的网站。

GridView1.DataSource = dt;
GridView1.DataBind();

我需要这个吗?这是做什么的?

我可以得到一些帮助的建议或链接,可以回答我的问题。谢谢你!

在asp.net c#中搜索gridview

下面是一个完整的工作示例。您可能希望稍微调整一下以满足自己的需求。正如Murray Foxcroft所指出的,在这个例子中,你会发现几个DataBindings使事情正常工作。

    <asp:TextBox ID="searchBox" runat="server"></asp:TextBox>
    <asp:Button ID="searchButton" runat="server" Text="search" OnClick="searchButton_Click" />
    <asp:Button ID="reset" runat="server" Text="reset" OnClick="resetSearchButton_Click" />
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="field01" HeaderText="Column A" />
            <asp:BoundField DataField="field02" HeaderText="Column B" />
            <asp:BoundField DataField="field03" HeaderText="Column C" />
        </Columns>
    </asp:GridView>

和后面的代码;

   protected void Page_Load(object sender, EventArgs e)
    {
        //to make sure the data isn't loaded in postback
        if (!Page.IsPostBack)
        {
            //use a datatable for storing all the data
            DataTable dt = new DataTable();
            string query = "SELECT * FROM myTable ORDER BY myColumn DESC";
            //wrapping in 'using' means the connection is closed an disposed when done
            using (SqlConnection connection = new SqlConnection("myConnectionString"))
            using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
            {
                try
                {
                    //fill the datatable with the contents from the database
                    adapter.Fill(dt);
                }
                catch
                {
                }
            }
            //save the datatable into a viewstate for later use
            ViewState["myViewState"] = dt;
            //bind the datasource to the gridview
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    protected void searchButton_Click(object sender, EventArgs e)
    {
        string searchTerm = searchBox.Text.ToLower();
        //check if the search input is at least 3 chars
        if (searchTerm.Length >= 3)
        {
            //always check if the viewstate exists before using it
            if (ViewState["myViewState"] == null)
                return;
            //cast the viewstate as a datatable
            DataTable dt = ViewState["myViewState"] as DataTable;
            //make a clone of the datatable
            DataTable dtNew = dt.Clone();
            //search the datatable for the correct fields
            foreach (DataRow row in dt.Rows)
            {
                //add your own columns to be searched here
                if (row["field01"].ToString().ToLower().Contains(searchTerm) || row["field02"].ToString().ToLower().Contains(searchTerm))
                {
                    //when found copy the row to the cloned table
                    dtNew.Rows.Add(row.ItemArray);
                }
            }
            //rebind the grid
            GridView1.DataSource = dtNew;
            GridView1.DataBind();
        }
    }

    protected void resetSearchButton_Click(object sender, EventArgs e)
    {
        //always check if the viewstate exists before using it
        if (ViewState["myViewState"] == null)
            return;
        //cast the viewstate as a datatable
        DataTable dt = ViewState["myViewState"] as DataTable;
        //rebind the grid
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

注意,gridview的搜索可能只有在少量的数据。如果你谈论的是1000+行,它会最好是搜索源(数据库)并将它们绑定到网格。

注释2:像这样搜索GridView单元格(Rows[0].Cells[1].Text)仅适用于BoundField列,而不是TemplateField和自动生成列。

你可以使用"DataTable" javascript功能来解决这个问题。请参考https://www.datatables.net/

$(document).ready(function(){
    $('#GridView1').DataTable();
});

只在这里设置你的表id,它会自动设置你的表的分页,排序,过滤。

谢谢