【问题标题】:searching gridview in asp.net C#在asp.net C#中搜索gridview
【发布时间】:2016-08-24 08:38:44
【问题描述】:

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

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

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

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

我能否获得一些可以回答我的问题的建议或链接。谢谢你。

【问题讨论】:

  • 是的,您需要将数据绑定到网格才能显示行。对象 dt 应该是一个数据表。然后您对数据表进行过滤(搜索)并重新绑定以显示匹配结果。这是一个相关的帖子stackoverflow.com/questions/5843537/…

标签: c# asp.net gridview


【解决方案1】:

这是一个完整的工作示例。你想稍微调整一下以满足你自己的需要。正如 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();
    }

请注意,这种对 GridViews 的搜索可能只对 少量数据。如果您谈论的是 1000 多行,它会 最好搜索源(数据库)并将它们绑定到 网格。

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

【讨论】:

  • 在 ViewState 中存储 500 多行数据有什么注意事项吗?任何应该知道的性能/安全问题?以这种方式使用 ViewState 有什么好处?我假设防止从数据库中多次加载数据,因为服务器已经在 ViewState 中拥有它?
  • 这取决于这 500 行包含多少数据(2 列 vs 200)。但是你需要知道所有的数据都默认存储在 ViewState 中。 ViewState 被编码,发送到客户端,使用 PostBack 发送回服务器,然后解码。因此,如果总数据为 100kb,则会产生 200kb 的额外流量。而且数据编码和解码需要时间。
  • 出于这个原因,我禁用了 ViewState ,它提供了极大的性能提升,并且一旦你习惯了没有生活的工作就会变得更好。唯一的区别是每个请求都会调用数据库,而不是只调用一次,然后将其存储在 ViewState 中,但是如果数据库设置正确,创建索引等,那么它会更快。
  • 感谢您的建议。因此,似乎多次查询一个健康的数据库比用视图状态敲击 Web 服务器和网络更好。很好。我的场景的数据大小是一个包含(截至目前)1000 行数据的单个数据库表,每行有 8 列。不大。简单的内网网页,通过用户 ID 搜索一个数据集并获取有关系统的一些信息。
  • 获取原始的精确副本,以便您可以将具有搜索词的行添加到新表中。您还可以在数据表上使用Linq,而无需克隆和循环所有行。两者都有效。
【解决方案2】:

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

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

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

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多