如何在Datagrid视图中进行高效搜索? [英] How Can I Do Efficient Search In Datagrid View ?

查看:57
本文介绍了如何在Datagrid视图中进行高效搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Windows窗体应用程序中使用数据网格视图。在我的应用程序中,我每次查询时都需要搜索data.i写入搜索查询文本框按键事件。我的应用程序在运行application.i时变慢一点我的表中有大约2000行数据。我可以更快地搜索并减少数据库负载。有没有选项可以在没有数据库交互的情况下搜索数据网格中的数据



我的代码在private void txtsearch_KeyDown(对象发送者,KeyEventArgs e){if(e.KeyCode == Keys.Down){datagridproduct.Focus(); } string constring = @Data Source = PC1 \WINMANERP; Initial Catalog = Easylife; Integrated Security = True; // if(e.KeyCode == Keys.Enter)// {



使用(SqlConnection con = new SqlConnection(constring))

{

使用(SqlCommand cmd = new SqlCommand(SELECT * FROM Item_Details,其中Item_Code LIKE'%'+ @NM +'%'或Item_Name LIKE'%'+ @NM +'%'或Supplier_Name LIKE'%'+ @ NM +'%'或位置LIKE'%'+ @NM +'%',con))

{

cmd.Parameters。 AddWithValue(@ NM,txtsearch.Text);

cmd.CommandType = CommandType.Text;

using(SqlDataAdapter sda = new SqlDataAdapter(cmd))

{

使用(DataTable dt = new DataTable())

{

sda.Fill(dt);

datagridproduct.DataSource = dt;

}

}

}







}









}

解决方案

1.任何网格视图控件都没有魔力。问题的解决方案是使用分页。



2.在我的下一篇文章中,我在Web应用程序的上下文中提供了解决此类问题的方法;但我提供了一个在数据库级别实现分页的存储过程(SP),因此您可以获取我的SP,根据您的上下文自定义它,然后在您的应用程序中使用它。以下是链接:高级ASPX GridView分页和数据实体 [ ^ ]

I am using data grid view in my windows form application.in my application i query every time when i need to search data.i wrote search query for text box key down event.my application gets little slower while running application.i have around 2000 rows of data in my table.how can i make my search faster and reduce database load .Is there any option to search data in data grid without database interaction

my code is below private void txtsearch_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Down) { datagridproduct.Focus(); } string constring = @"Data Source=PC1\WINMANERP;Initial Catalog=Easylife;Integrated Security=True"; //if (e.KeyCode == Keys.Enter) //{

using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Item_Details where Item_Code LIKE '%' + @NM + '%' OR Item_Name LIKE '%' + @NM + '%' OR Supplier_Name LIKE '%'+@NM+'%' OR Location LIKE '%' + @NM + '%'", con))
{
cmd.Parameters.AddWithValue("@NM", txtsearch.Text);
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
datagridproduct.DataSource = dt;
}
}
}



}




}

解决方案

1.There is no magic in any grid view control. The solution for your problem is to use pagination.

2.In my next article I provide solution for this kind of problem in the context of a web application; but I provide there a stored procedure (SP) that implement pagination at the database level, so you could get my SP, customize it for your context, then use it in your application. Here is the link: Advanced ASPX GridView Pagination and Data Entities[^]


这篇关于如何在Datagrid视图中进行高效搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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