Visual Studio C#: how to construct a form to search Access table content?

VAer 761 Reputation points
2020-12-31T12:08:16.073+00:00

Let us say, I am building a window form application.

I have a parent form (MdiContainer), which has MenuStrip. One menu will open a child form.

The bottom portion of child form is DataGridView, which shows Access table User, the table includes fields: UserID, FirstName, LastName, Title, Department, Phone, etc. So that user can view the whole table.

Example #1: The top portion of child form is search function, which will be used to search an employee. I want to find phone number for employee John Miller, but I am not sure if the last name Miller is accurate, maybe I can just enter "john" in search textbox, which will return all records including "john".
Example #2: I also want to have combo box, which can filter DataGridView employees based on combo box value, e.g. I want to find all employees in sale department, then I can select Sale as combo box value for Department field.

So I have two questions:

  1. In general, how is the idea? How to construct something for people to search an employee? Is DataGridView the right thing to use here?
  2. How to make textbox search-able? How to link textbox to DataGridView? I would like to learn more about it, so where should I begin.

Thanks.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,857 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,621 Reputation points
    2021-01-01T06:44:27.9+00:00

    Hi VAer-4038,
    You can search either FirstName or LastName with only one search textbox via following statement:

    "select * from Name where FirstName like '" + txt_SearchName.Text+ "%' or LastName like '" + txt_SearchName.Text + "%'"  
    

    And for question about filtering DataGridView employees based on combo box value, you can refer to the code below:

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)  
        {  
          
                (dataGridView1.DataSource as DataTable).DefaultView.RowFilter = string.Format("[Department] LIKE '%{0}%'", comboBox1.SelectedItem.ToString());  
         }  
    

    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Castorix31 82,216 Reputation points
    2020-12-31T13:24:39.9+00:00