在C#ASP.NET中使用文本框过滤GRIDVIEW [英] FILTER GRIDVIEW WITH TEXTBOX IN C# ASP.NET

查看:69
本文介绍了在C#ASP.NET中使用文本框过滤GRIDVIEW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我看到了几个带有下拉列表的示例,这些下拉列表在
中过滤结果 gridview.

有没有一种方法可以在用于过滤信息的文本框中键入信息
gridview?

例如,我想搜索一个人的名字和任何名字
包含我的文本框内容的名称出现在gridview列表中.


问候
Karthikeyan

Hello,

I have seen several examples with a dropdownlist filtering the results in a
gridview.

Is there a way to have information typed in a text box used to filter a
gridview?

I would like to for example search on a persons first name and any first
name that contains the contents of my text box appear in the gridview list.


Regards
Karthikeyan

推荐答案

例如,我想搜索一个人的名字和任何名字
包含我的文本框内容的名称将显示在gridview列表中.

里面是什么?继续做一个.您需要做的是在textchange事件中使用texbox的文本,并使用dataview过滤数据集,然后将网格与已过滤的dataview重新绑定.

尝试!
I would like to for example search on a persons first name and any first
name that contains the contents of my text box appear in the gridview list.

Whats in it? Go ahead and make one. All you need is to use the text of the texbox in the textchange event and filter the dataset using a dataview and then rebind the grid with the filtered dataview.

Try!


您需要在.aspx页面中添加此代码

崩溃
< asp:按钮ID ="btnSearch" runat =服务器" Text =点击我!"
onclick ="btnSearch_Click"/>



并且您需要在.aspx.cs页面中添加此代码

受保护的无效btnSearch_Click(对象发送者,EventArgs e)
{
SqlConnection sqlcon =新的SqlConnection(connstring);
字符串str = txtSearch.Text;
SqlCommand sqlcmd = new SqlCommand(从[表名]中选择*,其中字段名如"%"+ str +"%",sqlcon);
SqlDataAdapter adp =新的SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
adp.Fill(ds);
如果(ds.Tables [0] .Rows.Count> 0)
{
lbl1.Visible = false;
gvActivities.DataSource = ds.Tables [0];
gvActivities.DataBind();
}
其他
{
lbl1.Visible = true;
}
}
you need to add this code in the .aspx page

Collapse
<asp:Button ID="btnSearch" runat="server" Text="Click Me!"
onclick="btnSearch_Click" />



and you need to add this code in .aspx.cs page

protected void btnSearch_Click(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection(connstring);
string str = txtSearch.Text;
SqlCommand sqlcmd = new SqlCommand("select * from [table name] where fieldname like ''%" + str + "%''", sqlcon);
SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
adp.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
lbl1.Visible = false;
gvActivities.DataSource = ds.Tables[0];
gvActivities.DataBind();
}
else
{
lbl1.Visible = true;
}
}


这篇关于在C#ASP.NET中使用文本框过滤GRIDVIEW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆