Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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:

C#
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);
}
Posted
Updated 7-Feb-19 23:09pm
v2
Comments
F-ES Sitecore 8-Feb-19 5:03am    
Do the filtering in your SP. Pass the text to your usp_GetCustomers as a param and have that do the "where". Or create a new "usp_SearchCustomers" to do this.

1 solution

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}%'";
}
 
Share this answer
 
Comments
xdiet 8-Feb-19 22:30pm    
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900