none
grdiview的搜索问题 RRS feed

  • 问题

  • 各位大神:

    请问该如何实现grdiview的搜索功能呢?

    比如我输入一个值,然后我要搜索所有匹配的结果,显示在这个表中。

    请问我该怎么操作呢?

    非常感谢!
    2016年11月3日 6:40

答案

  • 你好,

    其实你可以使用数据库语言select like来实现你的需求。

    然后从后台重新绑定grdview。

    具体你可以参照如下代码:

    ASPX:

                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
                    <Columns>
                        <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
                        <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
                        <asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
                        <asp:BoundField DataField="CityId" HeaderText="CityId" SortExpression="CityId" />
                        <asp:BoundField DataField="ClassId" HeaderText="ClassId" SortExpression="ClassId" />
                        <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                    </Columns>
                </asp:GridView>
    
                <br />
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MysqlConnectionString %>" SelectCommand="SELECT * FROM [Userinfo]"></asp:SqlDataSource>
           

    后台代码:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    GridView1.DataSource = SqlDataSource1;
                    GridView1.DataBind();
                }
            }
    
    protected void Button1_Click(object sender, EventArgs e)
            {
                using (SqlConnection conn = new SqlConnection())
                {
                    conn.ConnectionString = ConfigurationManager
                            .ConnectionStrings["MysqlConnectionString"].ConnectionString;
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = "select * from [Userinfo] where " +
                        "name like @SearchText + '%'";
                        cmd.Parameters.AddWithValue("@SearchText", TextBox1.Text);
                        cmd.Connection = conn;
                        DataTable d1 = new DataTable();
                        SqlDataAdapter sda = new SqlDataAdapter(cmd);
                        sda.Fill(d1);
                        GridView1.DataSource = d1;
                        GridView1.DataBind(); 
                    }
                }
            }

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已建议为答案 Angie Xu 2016年11月10日 5:23
    • 已标记为答案 Anne_Wx 2016年11月14日 2:35
    2016年11月3日 12:46