如何使用文本框搜索datagridview(存储过程数据) [英] How to search datagridview (stored procedure data) using textbox

查看:92
本文介绍了如何使用文本框搜索datagridview(存储过程数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows应用程序中,我有一个Datagridview和Textbox控件。我正在使用存储过程在datagridview中显示一些数据。问题是,当我尝试在我的datagridview中搜索时,没有任何反应,当输入到文本框时它也是如此迟钝而且我发现了这个



In my Windows applicaton, I have a Datagridview and Textbox control. I'm using a stored procedure to display some data in the datagridview. The problem is when I tried to search in my datagridview, nothing happens and also it's so laggy when inputting to textbox and I discover this

CustomersList.DataSource = dt;



是原因。我很擅长使用存储过程。我希望有人能帮助我。



我尝试过:




is the reason. I'm pretty new to using stored procedures. I hope someone would be able to help me.

What I have tried:

Datatable dt;
private void txt_usersearch_TextChanged(object sender, EventArgs e)
{
    using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
        {
            using (var cmd = new SqlCommand("usp_GetCustomers", con))
            {
                cmd.CommandType = CommandType.StoredProcedure;                   
                dt.DefaultView.RowFilter = "Full_Name LIKE '%{txt_usersearch.Text}%'";
                CustomersList.DataSource = dt;
            }
        }
}

and this code is to display my data to datagridview

public class Display 
{
    public static void Display_Customer(DataTable dt, DataGridView dgv)
    {     
        using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["SalesInventoryManagement.Properties.Settings.Setting"].ConnectionString))
        {
            using (var cmd = new SqlCommand("usp_GetCustomers", con))
            {               
                con.Open();
                cmd.CommandType = CommandType.StoredProcedure;

                using (var sda = new SqlDataAdapter(cmd))
                {                        
                    dt = new DataTable();
                    sda.Fill(dt);
                    dgv.DataSource = dt;
                }

                con.Close();
            }
        }
    }  
}
Form load

private void ManageCustomer_Load(object sender, EventArgs e)
{
        Display.Display_Customer(dt, CustomersList);
}

推荐答案

你可以使用



var bs = new BindingSource;

bs.DataSource = dt;

dgv.DataSource = bs;



来填充你的Datagridview。



和:



private void txt_usersearch_TextChanged(object sender,EventArgs e)

{

bs.Filter =Full_Name LIKE'%{txt_usersearch.Text}%';

}
You can use

var bs = new BindingSource;
bs.DataSource = dt;
dgv.DataSource = bs;

to fill your Datagridview.

And:

private void txt_usersearch_TextChanged(object sender, EventArgs e)
{
bs.Filter = "Full_Name LIKE '%{txt_usersearch.Text}%'";
}


这篇关于如何使用文本框搜索datagridview(存储过程数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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