我正在尝试使用C#编程语言在asp.net中搜索网格视图。我不希望网格视图启用分页。我想提出输入的输入结果。例如。如果我输入' s,则以s开头的所有记录都只能显示。
我查了一些在代码中有数据绑定的网站。
GridView1.DataSource = dt;
GridView1.DataBind();
我需要这个吗?这是做什么的?
我可以获得一些可以回答我的问题的建议或链接的帮助。谢谢。
答案 0 :(得分: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和 AutoGenerated Columns。
答案 1 :(得分:0)
您可以使用“DataTable”javascript功能来解决此问题。请参阅https://www.datatables.net/。
$(document).ready(function(){
$('#GridView1').DataTable();
});
仅在此设置您的表格ID,它会自动设置您的表格的分页,排序和过滤。
由于