none
How to Search Datagridview (Stored Procedure Data) using Textbox RRS feed

  • Question

  • 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.

    Here's the code for searching

    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);
    }


    • Edited by Miracle_23 Friday, February 8, 2019 7:21 AM
    Friday, February 8, 2019 7:19 AM

All replies

  • Hi Miracle_23,

    After calling the method "Display.Display_Customer" when the form is loading, has the content in the DataGridView changed? If the content in the DataGridView has not changed just after triggering "txt_usersearch_TextChanged", you may try to assign "null" to the DataGridView's data source before setting the data source.

        cmd.CommandType = CommandType.StoredProcedure;
        dt.DefaultView.RowFilter = "Full_Name LIKE '%{txt_usersearch.Text}%'";
        CustomersList.DataSource = null;
        CustomersList.DataSource = dt;

    As for "so laggy when inputting to textbox", could you explain it in details?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 11, 2019 5:37 AM
    Moderator