如何根据搜索条件在gridview中显示数据 [英] how to display data in gridview based on the search criteria
本文介绍了如何根据搜索条件在gridview中显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个响应按键事件的文本框。当在文本框中输入一个字母时,它必须在网格视图中显示所有带有该字母的ID,例如(%d%)。它可以工作,但它会显示数据库中的所有数据,而不会根据字母进行排序。请帮帮我。
I have a text box that responds to a keypress event. When an letter is entered in the text box, it must display all ID''s with that letter in them e.g.(%d%) in a gridview. It works, but it displays all data in the database without sorting based on the letter. Please help me out.
private void txtCollectorid_KeyPress(object sender, KeyPressEventArgs e)
{
dgvContactpersonsearch.Visible = true;
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
string strConn = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
string strSQL = "select Collectorid AS ''Collector ID'', Title, Surname, Middlename AS ''Middle Name '', Firstname, Gender, Dateofbirth AS ''Date of Birth'' from tblcollectorsregistration where collectorid LIKE ''" + "%" + txtCollectorid.Text + "%" + "'' order by Surname";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strConn);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
bindingSource1.DataSource = table;
// you can make it grid readonly.
dgvContactpersonsearch.ReadOnly = true;
// finally bind the data to the grid
dgvContactpersonsearch.DataSource = bindingSource1;
}
推荐答案
试试这段代码。
Try this code.
private void txtCollectorid_KeyPress(object sender, KeyPressEventArgs e)
{
dgvContactpersonsearch.Visible = true;
SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
string strConn = "Data Source=user-PC;Initial Catalog=MUCGPROJECT;User ID=sa;Password=mike";
// Using this Query get the data from tblcollectorsregistration table
string strSQL = "select Collectorid AS ''Collector ID'', Title, Surname, Middlename AS ''Middle Name '', Firstname, Gender, Dateofbirth AS ''Date of Birth'' from tblcollectorsregistration order by Surname";
SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strConn);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
// Populate a new data table and bind it to the BindingSource.
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table); //Table is filled with data
//Now use below code to filter
string expression ="[Collector ID] like ''%" + txtCollectorid.Text + "%''";
//Make sure the ColumnName Collector ID is same as Displayed in the gridview.
DataRow[] filteredRows=table.Select(expression); //table is the Datatable
if (filteredRows.Any()) //If any rows are matching
{
table = filteredRows.CopyToDataTable();
}
// you can make it grid readonly.
dgvContactpersonsearch.ReadOnly = true;
// finally bind the data to the grid
dgvContactpersonsearch.DataSource = table ;
}
我想你已经在显示数据了加载表单时的gridview。为什么要再次调用数据库。
这是一个示例,您可以使用数据表作为Gridview的数据源。
Hi,
I think you are already displaying data in the gridview when loading the form.Why bother a call to the database again.
Here is an example where you can use the datatable which is used as datasource to the Gridview.
<pre lang="c#">
string expression ="[Collector ID] like '%" + txtCollectorid.Text + "%'";
//Make sure the ColumnName Collector ID is same as Displayed in the gridview.
DataRow[] filteredRows=table.Select(expression); //table is the Datatable
if (filteredRows.Any()) //If any rows are matching
{
dtfilter = filteredRows.CopyToDataTable();
}
通过这样做可以避免调用数据库。
by doing so you can avoid call to database.
这篇关于如何根据搜索条件在gridview中显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文