Dataview and Last keystroke

ankit goel 746 Reputation points
2023-05-14T13:20:35.11+00:00

Hi I have create a event for textbox which basically searches for a matching string by taking input from a textbox, filters the results from a dataset using a dataview and shows the results in a datagridview . now the problem is what is want is when the input string does not gives anything matching , it should not let the user type that key and also at the same time the already filter results should not change .
for example if a column name has names like
Ankit

Rajesh
yogesh

and if the user types A
it should give
Ankit
Rajesh
and when he types az
the system should not let the user types z as it would not yeild anything and at the same time , it should not change the search results which was
Ankit
Rajesh .

please modify my code .
thanks in advance

   private void customtextbox3_TextChanged(object sender, EventArgs e)
        {
            string searchText = customtextbox3.Text.Trim();
            DataView dataView = dataSet.Tables[0].DefaultView;

            if (searchText.Length > 0) // strings
            {
                dataView.RowFilter = $"name LIKE '%{searchText}%'";
                if (dataView.Count == 0) // check if no rows match the filter
                {
                    searchText = searchText.Substring(0, searchText.Length - 1);
                    customtextbox3.Text = searchText;
                }
                else
                {
                    
                    dataGridView1.DataSource = dataView;
                }
            }
            else
            {
               
                dataGridView1.DataSource = dataSet.Tables[0];
            }
        }

another problem that i am facing is , there is a logical error in my code as when the code reaches customtextbox3.Text = searchText; , this line self executes the customtextbox3_TextChanged method . please correct this .

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,869 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,604 questions
{count} votes