0

I am trying to search by name and by address in my datagridview the code is like below

  private void txtSearch_TextChanged(object sender, EventArgs e)
    {
        string searchText = txtSearch.Text.ToLower();
        List<EmplyesList> result = LoginData.employees.Where(t => t.FullName.ToLower().StartsWith(searchText)
         || t.Address.ToLower().StartsWith(searchText)).ToList();

        dgvEmployeeList.AutoGenerateColumns = false;
        dgvEmployeeList.DataSource = result;
    }

But when i try to search in the text box by writing a character it just clears the rows and doesnt show nothing .

I just cant see the problem that i am having

This is the result after trying to search

stuartd
  • 70,509
  • 14
  • 132
  • 163
  • I can't see anything wrong with your code either. I suggest you step through it in the debugger - does `LoginData.employees` have values? If you manually check in the watch windows or command window the result for `LoginData.employees.Where(t => t.FullName.ToLower().StartsWith("arb"))` does that return any results? Is the value of `searchText` what you expect it to be? – stuartd Jan 17 '20 at 22:08
  • You might need to do something like `dgvEmployeeList.Update();` followed by `dgvEmployeeList.Refresh();` after you update the datasource. – jwatts1980 Jan 17 '20 at 22:14
  • 1
    Might want to have a look at [Best way to refresh DataGridView when you update the base data source](https://stackoverflow.com/questions/253843/best-way-to-refresh-datagridview-when-you-update-the-base-data-source) – stuartd Jan 17 '20 at 22:16
  • yes `LoginData.employees` has values i have added 4 rows in dgv , When i manually check it doesn`t show anything ,Yes i expect it to be that way, I have actually used the same code in a different form with dgv and it always work but in this form it doesnt @stuartd – Arbnor Jusufi Jan 17 '20 at 22:30
  • Try `dgvEmployeeList.DataSource = null;` before `dgvEmployeeList.DataSource = result;` –  Jan 17 '20 at 23:48

0 Answers0